比较pd.Series并在该系列不包含None时得到异常结果 [英] Comparing pd.Series and getting, what appears to be, unusual results when the series contains None

查看:109
本文介绍了比较pd.Series并在该系列不包含None时得到异常结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么将两个相同的序列与None值进行比较会返回False:

I am wondering why comparing two identical series with None value returns False:

pd.Series(['x', 'y', None]) == pd.Series(['x', 'y', None])

0     True
1     True
2    False
dtype: bool

我希望所有结果都是正确的.如果我从系列中创建一个数组,然后进行比较,则会得到预期的结果:

I would expect all of results to be True. If I create an array, from the series, and compare I get the expected result:

pd.Series(['x', 'y', None]).values == pd.Series(['x', 'y', None]).values

array([ True,  True,  True])

为什么两个具有None的相同序列彼此不相等?我想念什么吗?

Why are the two identical series with None not equal to each other? Am I missing something?

我希望使用np.nan会出现这种情况,因为np.nan != np.nan;但是,None == None

I would expect this behavior with np.nan because np.nan != np.nan; however, None == None

推荐答案

这是

请参阅警告框: http://pandas.pydata.org/pandas-docs/stable/missing_data.html

这是很早以前完成的,以使行为为null 一致,因为它们不相等.这把Nonenp.nan相等(尽管与python不一致,但一致) numpy)立足.

This was done quite a while ago to make the behavior of nulls consistent, in that they don't compare equal. This puts None and np.nan on an equal (though not-consistent with python, BUT consistent with numpy) footing.

所以这不是错误,而是跨越2条约定的结果.

So this is not a bug, rather a consequence of stradling 2 conventions.

我认为文档可能会有所增强.

I suppose the documentation could be slightly enhanced.

对于包含空值的序列相等性,请使用 pd.Series.equals :

For equality of series containing null values, use pd.Series.equals:

pd.Series(['x', 'y', None]).equals(pd.Series(['x', 'y', None]))  # True

这篇关于比较pd.Series并在该系列不包含None时得到异常结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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