如何将 pandas DataFrame 与 Python 中的 None 进行比较?
How to compare pandas DataFrame against None in Python?
如何将 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'
我得到以下类型错误:
File "C:\Anaconda1\lib\site-packages\pandas\core\internals.py", line 885, in eval
% repr(other))
TypeError: Could not compare [None] with block values
使用is not
:
if self.pandas_df is not None:
print 'Do stuff'
PEP 8 说:
Comparisons to singletons like None
should always be done with is
or is not
, never the equality operators.
还有一个很好的 explanation 为什么。
如何将 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'
我得到以下类型错误:
File "C:\Anaconda1\lib\site-packages\pandas\core\internals.py", line 885, in eval
% repr(other))
TypeError: Could not compare [None] with block values
使用is not
:
if self.pandas_df is not None:
print 'Do stuff'
PEP 8 说:
Comparisons to singletons like
None
should always be done withis
oris not
, never the equality operators.
还有一个很好的 explanation 为什么。