Why python pandas does not use 3-valued logic? [duplicate]

旧城冷巷雨未停 提交于 2019-12-18 09:46:59

问题


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.

Example.

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

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

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?


回答1:


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 is "truthy".)




回答2:


You wrongly misjudged or and and statements.

or would check if first value is True in form of bool(value) if it's False then it takes second value.

and on the other hand checks if two of the values are True at the same time in the form of bool(value1) and bool(value2)



来源:https://stackoverflow.com/questions/43925797/why-python-pandas-does-not-use-3-valued-logic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!