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

How to compare pandas DataFrame against None in Python?

提问人:CiaranWelsh 提问时间:3/25/2016 最后编辑:Mike MüllerCiaranWelsh 更新时间:3/25/2016 访问量:22982

问:

如何将 pandas DataFrame 与 ?我有一个构造函数,它接受一个或一个,但从不同时接受。Noneparameter_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      

但是,当我稍后尝试比较 against 时(即当实际包含 pandas 数据帧时):pandas_dfNoneself.pandas_df

    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
python pandas python-2.x 无类型

评论


答:

61赞 Mike Müller 3/25/2016 #1

用:is not

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

PEP 8 说:

与单例的比较应该始终使用 or ,而不是相等运算符。Noneisis not

还有一个很好的解释