在尝试对可能包含nan
值的数据(如 numpy 中定义)求逻辑表达式时,我得到了一些令人惊讶的结果。
我想了解为什么会出现这种结果以及如何以正确的方式实施。
我不明白为什么这些表达式的计算结果是它们所做的值:
from numpy import nan
nan and True
>>> True
# this is wrong.. I would expect to evaluate to nan
True and nan
>>> nan
# OK
nan and False
>>> False
# OK regardless the value of the first element
# the expression should evaluate to False
False and nan
>>> False
#ok
同样对于or
:
True or nan
>>> True #OK
nan or True
>>> nan #wrong the expression is True
False or nan
>>> nan #OK
nan or False
>>> nan #OK
如何(以有效的方式)实现正确的布尔函数,同时处理nan
值?