Comparing two pandas series for floating point near-equality?

You can use numpy.allclose:

numpy.allclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False)

Returns True if two arrays are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.

numpy works well with pandas.Series objects, so if you have two of them - s1 and s2, you can simply do:

np.allclose(s1, s2, atol=...) 

Where atol is your tolerance value.


Numpy works well with pandas Series. However one has to be careful with the order of indices (or columns and indices for pandas DataFrame)

For example

series_1 = pd.Series(data=[0,1], index=['a','b'])
series_2 = pd.Series(data=[1,0], index=['b','a']) 
np.allclose(series_1,series_2)

will return False

A workaround is to use the index of one pandas series

np.allclose(series_1, series_2.loc[series_1.index])