为什么python pandas不使用三值逻辑? [英] Why python pandas does not use 3-valued logic?

查看:95
本文介绍了为什么python pandas不使用三值逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道python pandas/numpy为什么不实现带有true,false和NA的3值逻辑(所谓的Łukasiewicz逻辑)(例如R).我已阅读( https://www.oreilly.com/learning/handling-missing -data ),这在一定程度上是由于大熊猫使用的基本数据类型比R多得多.但是,对于我来说,这还不是很清楚,为什么在这种情况下,不可避免地会出现这种逻辑操作的奇怪行为,即缺少值.

I wonder why does python pandas / numpy not implement 3-valued logic (so-called Łukasiewicz's logic) with true, false and NA (like for instance R does). I've read (https://www.oreilly.com/learning/handling-missing-data) that this is to some extent due to the fact that pandas uses much more many basic data types than R for example. However, this is not entirely clear to me why in this case it is unavoidable to have this weird behaviour of logical operations with missing values.

示例.

import numpy as np
np.nan and False   # so far so good, we have False
np.nan or False    # again, good, we have nan
False and np.nan   # False, good
False or np.nan    # give nan, so again, it is correct
np.nan and True    # weird, this gives True, while it should give nan
True and np.nan    # nan, so it is correct, but switching order should not affect the result
np.nan or True     # gives nan, which is not correct, should be True
True or np.nan     # True so it is correct, again switching the arguments changes the result

因此,该示例显示在np.nanTrue值之间的比较中发生了一些非常奇怪的事情.那么这是怎么回事?

So the example shows that something very weird happens in comparisons between np.nan and True values. So what is going on here?

编辑. 感谢您的评论,现在我看到np.nan被视为真实"值.那么,谁能解释这到底是什么意思,以及这种方法背后的原理是什么?

EDIT. Thanks for the comments, now I see that np.nan is considered a "truthy" value. So can anybody explain what does this mean exactly and what is a rationale behind this approach?

推荐答案

这是numpy行为,至少部分是从python继承的:

This is numpy behaviour and, at least partially, inherited from python:

In [11]: bool(float('nan'))
Out[11]: True

In [12]: bool(np.NaN)
Out[12]: True

(NaN是真实的".)

这篇关于为什么python pandas不使用三值逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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