如何将pandas DataFrame与None
进行比较?我有一个构造函数,它接受parameter_file
或pandas_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_df
与None
进行比较时,(当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
答案 0 :(得分:43)
使用is not
:
if self.pandas_df is not None:
print 'Do stuff'
PEP 8说:
与
None
等单身人士的比较应始终使用is
或is not
,而不是等同运算符。
还有一个很好的explanation为什么。