Skip to content

Commit 0057ee2

Browse files
jbrockmendelharisbal
authored and
harisbal
committed
templatize timedelta arith ops (pandas-dev#19871)
1 parent 92299cd commit 0057ee2

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

pandas/_libs/tslibs/timedeltas.pyx

+40-7
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,12 @@ def _binary_op_method_timedeltalike(op, name):
471471
# define a binary operation that only works if the other argument is
472472
# timedelta like or an array of timedeltalike
473473
def f(self, other):
474-
if hasattr(other, 'delta') and not PyDelta_Check(other):
475-
# offsets.Tick
476-
return op(self, other.delta)
474+
if hasattr(other, '_typ'):
475+
# Series, DataFrame, ...
476+
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
477+
# Tick offset
478+
return op(self, other.delta)
479+
return NotImplemented
477480

478481
elif other is NaT:
479482
return NaT
@@ -1052,7 +1055,14 @@ class Timedelta(_Timedelta):
10521055
__rsub__ = _binary_op_method_timedeltalike(lambda x, y: y - x, '__rsub__')
10531056

10541057
def __mul__(self, other):
1055-
if hasattr(other, 'dtype'):
1058+
if hasattr(other, '_typ'):
1059+
# Series, DataFrame, ...
1060+
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
1061+
# Tick offset; this op will raise TypeError
1062+
return other.delta * self
1063+
return NotImplemented
1064+
1065+
elif hasattr(other, 'dtype'):
10561066
# ndarray-like
10571067
return other * self.to_timedelta64()
10581068

@@ -1068,7 +1078,18 @@ class Timedelta(_Timedelta):
10681078
__rmul__ = __mul__
10691079

10701080
def __truediv__(self, other):
1071-
if hasattr(other, 'dtype'):
1081+
if hasattr(other, '_typ'):
1082+
# Series, DataFrame, ...
1083+
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
1084+
# Tick offset
1085+
return self / other.delta
1086+
return NotImplemented
1087+
1088+
elif is_timedelta64_object(other):
1089+
# convert to Timedelta below
1090+
pass
1091+
1092+
elif hasattr(other, 'dtype'):
10721093
return self.to_timedelta64() / other
10731094

10741095
elif is_integer_object(other) or is_float_object(other):
@@ -1084,7 +1105,18 @@ class Timedelta(_Timedelta):
10841105
return self.value / float(other.value)
10851106

10861107
def __rtruediv__(self, other):
1087-
if hasattr(other, 'dtype'):
1108+
if hasattr(other, '_typ'):
1109+
# Series, DataFrame, ...
1110+
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
1111+
# Tick offset
1112+
return other.delta / self
1113+
return NotImplemented
1114+
1115+
elif is_timedelta64_object(other):
1116+
# convert to Timedelta below
1117+
pass
1118+
1119+
elif hasattr(other, 'dtype'):
10881120
return other / self.to_timedelta64()
10891121

10901122
elif not _validate_ops_compat(other):
@@ -1160,9 +1192,10 @@ class Timedelta(_Timedelta):
11601192
'{op}'.format(dtype=other.dtype,
11611193
op='__floordiv__'))
11621194

1163-
if is_float_object(other) and util._checknull(other):
1195+
elif is_float_object(other) and util._checknull(other):
11641196
# i.e. np.nan
11651197
return NotImplemented
1198+
11661199
elif not _validate_ops_compat(other):
11671200
return NotImplemented
11681201

0 commit comments

Comments
 (0)