Why does pandas “None | True” return False when Python “None or True” returns True?

21

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.

Share
Improve this question
18
  • 11
    | and or are two entirely different operators. Note that None | True produces a type error. – chepner Apr 6 at 14:35
  • 4
    @chepner: Yeah, but Pandas uses | for logical or, and we're not getting a TypeError. We're getting False somehow. – user2357112 supports Monica Apr 6 at 14:37
  • 3
    Pandas doc (pandas.pydata.org/pandas-docs/stable/user_guide/…) specifies that | is used for logical or and not bitwise or. My pandas version is 1.2.0 – politinsa Apr 6 at 14:39
  • 3
    @CharlesDuffy I don't see the question as that type of why. This why is more of a "This code does something else from what I would expect. What am I overlooking? Where is my mistake?" which to me seems like a very common and meaningful type of question on Stack Overflow. And pointing to how the or operators are defined in pandas, or what bug this behaviour is a consequence of (I don't know which is the case), would answer the question. The OP doesn't ask why the operators are defined like that or why there is a bug; only in those cases would it be a why of the type you mention. – Jesper Apr 9 at 14:43
  • 3
    @Jesper, I generally agree; it's that the comments asserting that there is a bug were ignored / treated as nonresponsive by the OP (and the question had a bounty added with a message refocusing on the interest being an explanation rather than a workaround) that led to the above comment. – Charles Duffy Apr 9 at 17:41

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue