如何在Python中将pandas DataFrame与None进行比较?

时间:2016-03-25 10:13:46

标签: python pandas python-2.x nonetype

如何将pandas DataFrame与None进行比较?我有一个构造函数,它接受parameter_filepandas_df之一,但从不两者。

def __init__(self,copasi_file,row_to_insert=0,parameter_file=None,pandas_df=None):
    self.copasi_file=copasi_file
    self.parameter_file=parameter_file
    self.pandas_df=pandas_df      

但是,当我稍后尝试将pandas_dfNone进行比较时,(当self.pandas_df实际包含pandas数据帧时):

    if self.pandas_df!=None:
        print 'Do stuff'

我得到以下TypeError:

  File "C:\Anaconda1\lib\site-packages\pandas\core\internals.py", line 885, in eval
    % repr(other))

TypeError: Could not compare [None] with block values

1 个答案:

答案 0 :(得分:43)

使用is not

if self.pandas_df is not None:
    print 'Do stuff'

PEP 8说:

  

None等单身人士的比较应始终使用isis not,而不是等同运算符。

还有一个很好的explanation为什么。