pythonpandasnumpynanequality-operator

Comparing pandas Series for equality when they contain nan?


My application needs to compare Series instances that sometimes contain nans. That causes ordinary comparison using == to fail, since nan != nan:

import numpy as np
from pandas import Series
s1 = Series([1,np.nan])
s2 = Series([1,np.nan])

>>> (Series([1, nan]) == Series([1, nan])).all()
False

What's the proper way to compare such Series?


Solution

  • How about this. First check the NaNs are in the same place (using isnull):

    In [11]: s1.isnull()
    Out[11]: 
    0    False
    1     True
    dtype: bool
    
    In [12]: s1.isnull() == s2.isnull()
    Out[12]: 
    0    True
    1    True
    dtype: bool
    

    Then check the values which aren't NaN are equal (using notnull):

    In [13]: s1[s1.notnull()]
    Out[13]: 
    0    1
    dtype: float64
    
    In [14]: s1[s1.notnull()] == s2[s2.notnull()]
    Out[14]: 
    0    True
    dtype: bool
    

    In order to be equal we need both to be True:

    In [15]: (s1.isnull() == s2.isnull()).all() and (s1[s1.notnull()] == s2[s2.notnull()]).all()
    Out[15]: True
    

    You could also check name etc. if this wasn't sufficient.

    If you want to raise if they are different, use assert_series_equal from pandas.util.testing:

    In [21]: from pandas.util.testing import assert_series_equal
    
    In [22]: assert_series_equal(s1, s2)