Skip to content

templatize timedelta arith ops #19871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 40 additions & 7 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,12 @@ def _binary_op_method_timedeltalike(op, name):
# define a binary operation that only works if the other argument is
# timedelta like or an array of timedeltalike
def f(self, other):
if hasattr(other, 'delta') and not PyDelta_Check(other):
# offsets.Tick
return op(self, other.delta)
if hasattr(other, '_typ'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than repeating this code can you make a function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats exactly what this PR is preliminary to, but with more than just these 3 lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh ok

# Series, DataFrame, ...
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
# Tick offset
return op(self, other.delta)
return NotImplemented

elif other is NaT:
return NaT
Expand Down Expand Up @@ -1052,7 +1055,14 @@ class Timedelta(_Timedelta):
__rsub__ = _binary_op_method_timedeltalike(lambda x, y: y - x, '__rsub__')

def __mul__(self, other):
if hasattr(other, 'dtype'):
if hasattr(other, '_typ'):
# Series, DataFrame, ...
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
# Tick offset; this op will raise TypeError
return other.delta * self
return NotImplemented

elif hasattr(other, 'dtype'):
# ndarray-like
return other * self.to_timedelta64()

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

def __truediv__(self, other):
if hasattr(other, 'dtype'):
if hasattr(other, '_typ'):
# Series, DataFrame, ...
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
# Tick offset
return self / other.delta
return NotImplemented

elif is_timedelta64_object(other):
# convert to Timedelta below
pass

elif hasattr(other, 'dtype'):
return self.to_timedelta64() / other

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

def __rtruediv__(self, other):
if hasattr(other, 'dtype'):
if hasattr(other, '_typ'):
# Series, DataFrame, ...
if other._typ == 'dateoffset' and hasattr(other, 'delta'):
# Tick offset
return other.delta / self
return NotImplemented

elif is_timedelta64_object(other):
# convert to Timedelta below
pass

elif hasattr(other, 'dtype'):
return other / self.to_timedelta64()

elif not _validate_ops_compat(other):
Expand Down Expand Up @@ -1160,9 +1192,10 @@ class Timedelta(_Timedelta):
'{op}'.format(dtype=other.dtype,
op='__floordiv__'))

if is_float_object(other) and util._checknull(other):
elif is_float_object(other) and util._checknull(other):
# i.e. np.nan
return NotImplemented

elif not _validate_ops_compat(other):
return NotImplemented

Expand Down