I am trying to subclass pandas' Timedelta
so when it gets summed by a integer/float, we consider it as a Timedelta
with seconds.
I tried the following:
class Timedelta(pd.Timedelta):
def __add__(self, other):
print("OVERRIDEN ADD")
try:
print("SUPER ADD")
return super().__add__(other)
except:
print("NEW ADD")
return super().__add__(Timedelta(str(other)+"s"))
But for some reason I can't get it to go to the "NEW ADD"
implementation:
>>> a = Timedelta2('10s')
>>> a+1
OVERRIDEN ADD
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
SUPER ADD
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-51-98b939904c8e>", line 1, in <module>
a+1
TypeError: unsupported operand type(s) for +: 'Timedelta2' and 'int'
I was expecting this TypeError
exception to be caught, but for some reason it isn't, so I would like to ask for some help to figure out what could be happening here.
Thanks!