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?
How about this. First check the NaNs are in the same place (using isnull):
Then check the values which aren't NaN are equal (using notnull):
In order to be equal we need both to be 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
frompandas.util.testing
: