Python numpy.nan and boolean functions: incorrect results

I get unexpected results when trying to evaluate logical expressions for data that may contain nan values ​​(as defined in numpy).

I would like to understand why these results arise and how to implement the right way.

I do not understand why these expressions evaluate the meaning that they fulfill:

 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 

Similarly for 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 

How can I (efficiently) implement the correct logical functions while also processing nan values?

+4
python numpy boolean nan
source share
2 answers

You can use predicates from the numpy namespace:

 >>> np.logical_and(True, np.nan), np.logical_and(False, np.nan) (True, False) >>> np.logical_and(np.nan, True), np.logical_and(np.nan, False) (True, False) >>> >>> np.logical_or(True, np.nan), np.logical_or(False, np.nan) (True, True) >>> np.logical_or(np.nan, True), np.logical_or(np.nan, False) (True, True) 

EDIT: The built-in Boolean operators are slightly different. From the docs : x and y equivalent to if x is false, then x, else y . So, if the first argument evaluates to False , they return it (and not its logical equivalent, as it were). Therefore:

 >>> (None and True) is None True >>> [] and True [] >>> [] and False [] >>> 

etc.

+4
source share

When evaluating logical expressions containing and , we must evaluate the expressions that are present on both sides of the and operator. If for the or operator, if the first expression is True, then there is no need to verify the correctness of the second expression

For example, evaluating the expression 2>2 and 3==3 , you first need to check whether the first expression 2>2 True or not. If this first expression is False, then there is no need to check the second expression because of the and operator, and the result of this expression will be FALSE, since the first expression is FALSE. If the expression was 2==2 AND 3==3 , then, since the first expression 2==2 is True, we do not need to check the correctness of the second expression, and since the second expression is also true here, we get TRUE as output.

In nan and True , since nan is True and because of the and operator, python now evaluates the second expression and returns the value of the second expression. So here you get TRUE as output. The same logic, when applied to True and nan , you can expect nan as output.

The or operator is enough if we look at the first expression, so " True or nan will return True

0
source share

All Articles