How to compare pandas DataFrame against None in Python?

Question:

How do I compare a pandas DataFrame with None? I have a constructor that takes one of a parameter_file or a pandas_df but never both.

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      

However, when I later try to compare the pandas_df against None, (i.e. when self.pandas_df actually contains a pandas dataframe):

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

I get the following TypeError:

  File "C:Anaconda1libsite-packagespandascoreinternals.py", line 885, in eval
    % repr(other))

TypeError: Could not compare [None] with block values
Asked By: CiaranWelsh

||

Answers:

Use is not:

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

PEP 8 says:

Comparisons to singletons like None should always be done with is or is not, never the equality operators.

There is also a nice explanation why.

Answered By: Mike Müller
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.