In pure Python, None or True
returns True
.
However with pandas when I'm doing a |
between two Series containing None values, results are not as I expected:
>>> df.to_dict()
{'buybox': {0: None}, 'buybox_y': {0: True}}
>>> df
buybox buybox_y
0 None True
>>> df['buybox'] = (df['buybox'] | df['buybox_y'])
>>> df
buybox buybox_y
0 False True
Expected result:
>>> df
buybox buybox_y
0 True True
I get the result I want by applying the OR operation twice, but I don't get why I should do this.
I'm not looking for a workaround (I have it by applying df['buybox'] = (df['buybox'] | df['buybox_y'])
twice in a row) but an explanation, thus the 'why' in the title.
|
andor
are two entirely different operators. Note thatNone | True
produces a type error. – Septimal|
for logical or, and we're not getting a TypeError. We're getting False somehow. – Bratton|
is used for logical or and not bitwise or. My pandas version is 1.2.0 – Demarsdf.any(axis=1)
works somehow :-). – Husted__or__
is implemented to convertNone
to abool
first.or
isn't really a boolean operator, but it uses boolean equivalents to determine which argument to return. – SeptimalNone
is interpreted as truthy when evaluating the or|
and as falsey when converted to boolean. The second part is easy to verify asdf['buybox'].astype(bool)
gets toFalse
. – Outfitpandas.Series([True]) | pandas.Series([nan])
has aTrue
instead ofFalse
in the result. (Putting the NaN first gives False.) – BrattonNone
were treated as truthy in the|
, then we'd get True, not False. – Bratton1 or True -> 1
. Likely,|
is short-circuiting and not even caring what is on the other side, as your finding of swapping the order of operands suggests. – Outfit'boolean'
dtype seems to have NaN treated properly. – Choate