Code Sample, a copy-pastable example if possible
In [1]: class Test:
...: def __ror__(self, other):
...: print('here')
...: return self
...:
In [2]: {} | Test()
here
Out[2]: <__main__.Test at 0x7f37a1601ba8>
In [3]: import pandas
In [4]: pandas.DataFrame() | Test()
Out[4]:
Empty DataFrame
Columns: []
Index: []
Problem description
I would expect DataFrame to return NotImplemented
in this case, so we can properly delegate to __ror__
.
Expected Output
Out[3]: <__main__.Test at 0x7f37a1601ba8>
Output of pd.show_versions()
Comment From: gfyoung
cc @jreback
Comment From: TomAugspurger
I didn't know this, but apparently DataFrame.__or__
will fill fill missing values with other
, for non series / frame.
In [6]: pd.DataFrame({"A": [np.nan]}) | Test()
here
Out[6]:
A
0 True
This is a bit strange, but is it worth deprecating?
Comment From: WillAyd
I think this is happening here:
https://github.com/pandas-dev/pandas/blob/ee6b1318ff07806ead79dac8119ad9ce580afe1c/pandas/core/ops.py#L2167
There's a comment elsewhere in the module about how the bool_method objects are not tested, so I think this is a pretty deep bug.
Not sure what the best solution is yet but providing as FYI in case it helps others to diagnose
Comment From: jbrockmendel
This can be done by implementing __pandas_priority__
on your custom class. Closed by #48347.