Pandas version checks
- [X] I have checked that this issue has not already been reported.
- [X] I have confirmed this bug exists on the latest version of pandas.
- [X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
df = pd.DataFrame([{"a": True}, {"b":True}])
df["a"]|df["b"]
Issue Description
The result of a bitwise or operator (|
or pd.Series.__or__()
) should not depend on the order of the operands. Or should be commutative.
But in this case, when one of the operands is NaN, pandas violates this law. This seems odd. Numpy doesn't show this inconsistency. Numpy complains that NaN cannot be used with Or.
Pandas silently casts, without any warning.
There's been a StackOverflow issue about this for a long time, but I couldn't find an issue here: https://stackoverflow.com/questions/39000907/pandas-column-selection-non-commutative-bitwise-or-when-selecting-on-str-and-na
Expected Behavior
The result should always be true in bitwise or comparison if one of the operands is true.
It's ok if the user gets a warning or there's an error. But silently making it go false is bad.
Actual behavior
> df
a b
0 True NaN
1 NaN True
> df["a"]|df["b"]
0 True
1 False
dtype: bool
Installed Versions
Comment From: corneliusroemer
Possible duplicate of https://github.com/pandas-dev/pandas/issues/41604
Comment From: topper-123
This is a bit of edge case for pandas as generally (outside of pandas) we have that
>>> True | 1.5
TypeError: unsupported operand type(s) for |: 'bool' and 'float'
>>> True | np.nan
TypeError: unsupported operand type(s) for |: 'bool' and 'float'
>>> True | None
TypeError: unsupported operand type(s) for |: 'bool' and 'NoneType'
So in this case you are working with object dtypes with incompatible data, which is never fun.
We do have some half-baked support for this, but I'd strongly recommend convert to "boolean" dtype when working with booleans with nan data.
So IDK, it's probably almost hopeless to support all "expected" cases when using object dtypes. I guess you could try to see if you can patch this to work, but I think it has to be relatively simple to be accepted.