4

I am testing for np.nan values in a column of a pandas dataframe.

When I do: df[df['col1'] == np.nan] I get all False values even in rows where there is a NaN.

However, np.isnan(df['col1']) works as expected.

So why the discrepancy and why does the former not work.

Barmar
  • 741,623
  • 53
  • 500
  • 612
codingknob
  • 11,108
  • 25
  • 89
  • 126
  • 2
    Nothing is equal to `NaN`, not even `NaN`. That's why there's `isnan()`. – kindall Sep 23 '16 at 17:01
  • 3
    As a side note: `pd.isnull` is more robust than `np.isnan` when working with DataFrames. You should probably choose `pd.isnull` (or just `df['col1'].isnull()`) over `np.isnan`. – root Sep 23 '16 at 17:04
  • np.isnan does not work for string values. You right right pd.isnull is best for DataFrames and the different dtypes – codingknob Sep 23 '16 at 19:03

0 Answers0