当尝试对可能包含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值?
隔江千里
相关分类