python-比较pd.Series并在该系列不包含None时获得异常结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-比较pd.Series并在该系列不包含None时获得异常结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含970字,纯文字阅读大概需要2分钟。
内容图文
![python-比较pd.Series并在该系列不包含None时获得异常结果](/upload/InfoBanner/zyjiaocheng/670/1723462abe794ec084ed2d883b448868.jpg)
我想知道为什么将两个相同的系列与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
内容总结
以上是互联网集市为您收集整理的python-比较pd.Series并在该系列不包含None时获得异常结果全部内容,希望文章能够帮你解决python-比较pd.Series并在该系列不包含None时获得异常结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。