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:\Anaconda1\lib\site-packages\pandas\core\internals.py", line 885, in eval % repr(other)) TypeError: Could not compare [None] with block values
None is also considered a missing value In pandas, None is also treated as a missing value. None is a built-in constant in Python. For numeric columns, None is converted to nan when a DataFrame or Series containing None is created, or None is assigned to an element.
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 withis
oris not
, never the equality operators.
There is also a nice explanation why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With