我想知道为什么将两个相同的系列与None值进行比较会返回False:
pd.Series(['x', 'y', None]) == pd.Series(['x', 'y', None])
0 True
1 True
2 False
dtype: bool
我希望所有结果都是正确的.如果我从系列中创建一个数组,并进行比较,我将得到预期的结果:
pd.Series(['x', 'y', None]).values == pd.Series(['x', 'y', None]).values
array([ True, True, True])
为什么没有的两个相同的序列彼此不相等?我想念什么吗?
我希望np.nan会出现这种情况,因为np.nan!= np.nan;但是,无==无
解决方法:
这是by design:
see the warnings Box: 07001
This was done quite a while ago to make the behavior of nulls
consistent, in that they don’t compare equal. This putsNone
and
np.nan
on an equal (though not-consistent with python, BUT consistent
with numpy) footing.So this is not a bug, rather a consequence of sTradling 2 conventions.
I suppose the documentation Could be slightly enhanced.
要使包含空值的级数相等,请使用pd.Series.equals
:
pd.Series(['x', 'y', None]).equals(pd.Series(['x', 'y', None])) # True