Jak
Jak

Reputation: 19

Different pandas DataFrame logical operation result when the changing the order

My code is like:

a = pd.DataFrame([np.nan, True])
b = pd.DataFrame([True, np.nan])
c = a|b
print(c)

I don't know the result of logical operation result when one element is np.nan, but I expect them to be the same whatever the oder. But I got the result like this:

       0
0  False
1   True

Why? Is this about short circuiting in pandas? I searched the doc of pandas but did not find answer.

My pandas version is 1.1.3

Upvotes: 0

Views: 42

Answers (1)

Philip Ciunkiewicz
Philip Ciunkiewicz

Reputation: 2791

This is behaviour that is tied to np.nan, not pandas. Take the following examples:

print(True or np.nan)
print(np.nan or True)

Output:

True
nan

When performing the operation, dtype ends up mattering and the way that np.nan functions within the numpy library is what leads to this strange behaviour.

To get around this quirk, you can fill NaN values with False for example or some other token value which evaluates to False (using pandas.DataFrame.fillna()).

Upvotes: 2

Related Questions