比较大 pandas 系列是否包含nan时是否相等? [英] Comparing pandas Series for equality when they contain nan?

查看:96
本文介绍了比较大 pandas 系列是否包含nan时是否相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序需要比较有时包含nan的Series实例.这导致使用==的普通比较失败,因为nan != 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?

推荐答案

如何处理.首先检查NaN是否在同一位置(使用 isnull ):

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

然后检查非NaN的值是否相等(使用

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

为了相等,我们都必须为True:

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

如果还不够,还可以检查姓名等.

如果您想提高,如果它们不同,请使用

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)

这篇关于比较大 pandas 系列是否包含nan时是否相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆