Comparing pandas Series for equality when they contain nan?

11.4k views Asked by At

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?

4

There are 4 answers

5
Andy Hayden On BEST ANSWER

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)
1
Jeff On
In [16]: s1 = Series([1,np.nan])

In [17]: s2 = Series([1,np.nan])

In [18]: (s1.dropna()==s2.dropna()).all()
Out[18]: True
0
Sam On

Currently one should just use series1.equals(series2) see docs. This also checks if nans are in the same positions.

0
Steven Van Ingelgem On

I came looking here for a similar answer, and think @Sam's answer is the neatest if you just want 1 value back. But I wanted a truth-array back with an element-wise comparison (but null safe).

So finally I ended up with:

import pandas as pd

s1 = pd.Series([1,np.nan, 2, np.nan])
s2 = pd.Series([1,np.nan, np.nan, 2])

(s1 == s2) | ~(s1.isnull() ^ s2.isnull())

The result:

0     True
1     True
2    False
3    False
dtype: bool

Comparing this to s1 == s2:

0     True
1    False
2    False
3    False
dtype: bool