I am trying to subtract two dataframes.
The logic I have for __sub__ behaves as expected, but the logic for __rsub__ does not output the reverse.
How can I get the __rsub__ output to match?
import pandas as pd
class Base:
def __init__(self):
self.data = pd.DataFrame({'a': [5, 6]})
def __sub__(self, other):
return self.data - other
def __rsub__(self, other):
return other - self.data
base = Base()
df = pd.DataFrame({'a': [1, 2]})
# Unexpected output
>>> df - base
a
0 a
0 -4
1 -5
1 a
0 -3
1 -4
# Expected output
>>> base - df
a
0 4
1 4