Skip to content

ENH: Timedelta division support non-nano #47373

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 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
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
52 changes: 35 additions & 17 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1742,13 +1742,21 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if other._reso != self._reso:
raise ValueError(
"division between Timedeltas with mismatched resolutions "
"are not supported. Explicitly cast to matching resolutions "
"before dividing."
)
return self.value / float(other.value)

elif is_integer_object(other) or is_float_object(other):
# integers or floats
if self._reso != NPY_FR_ns:
raise NotImplementedError
return Timedelta(self.value / other, unit='ns')
if util.is_nan(other):
return NaT
return Timedelta._from_value_and_reso(
<int64_t>(self.value / other), self._reso
)

elif is_array(other):
return self.to_timedelta64() / other
Expand All @@ -1761,8 +1769,12 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if self._reso != NPY_FR_ns:
raise NotImplementedError
if self._reso != other._reso:
raise ValueError(
"division between Timedeltas with mismatched resolutions "
"are not supported. Explicitly cast to matching resolutions "
"before dividing."
)
return float(other.value) / self.value

elif is_array(other):
Expand All @@ -1781,14 +1793,18 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if self._reso != NPY_FR_ns:
raise NotImplementedError
if self._reso != other._reso:
raise ValueError(
"floordivision between Timedeltas with mismatched resolutions "
"are not supported. Explicitly cast to matching resolutions "
"before dividing."
)
return self.value // other.value

elif is_integer_object(other) or is_float_object(other):
if self._reso != NPY_FR_ns:
raise NotImplementedError
return Timedelta(self.value // other, unit='ns')
if util.is_nan(other):
return NaT
return type(self)._from_value_and_reso(self.value // other, self._reso)

elif is_array(other):
if other.dtype.kind == 'm':
Expand All @@ -1798,9 +1814,7 @@ class Timedelta(_Timedelta):
return _broadcast_floordiv_td64(self.value, other, _floordiv)
elif other.dtype.kind in ['i', 'u', 'f']:
if other.ndim == 0:
if self._reso != NPY_FR_ns:
raise NotImplementedError
return Timedelta(self.value // other)
return self // other.item()
else:
return self.to_timedelta64() // other

Expand All @@ -1816,8 +1830,12 @@ class Timedelta(_Timedelta):
other = Timedelta(other)
if other is NaT:
return np.nan
if self._reso != NPY_FR_ns:
raise NotImplementedError
if self._reso != other._reso:
raise ValueError(
"floordivision between Timedeltas with mismatched resolutions "
"are not supported. Explicitly cast to matching resolutions "
"before dividing."
)
return other.value // self.value

elif is_array(other):
Expand Down Expand Up @@ -1914,10 +1932,10 @@ cdef _broadcast_floordiv_td64(
if mask:
return np.nan

return operation(value, other.astype('m8[ns]').astype('i8'))
return operation(value, other.astype('m8[ns]', copy=False).astype('i8'))

else:
res = operation(value, other.astype('m8[ns]').astype('i8'))
res = operation(value, other.astype('m8[ns]', copy=False).astype('i8'))

if mask.any():
res = res.astype('f8')
Expand Down
58 changes: 58 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,64 @@ def test_to_timedelta64(self, td, unit):
elif unit == 9:
assert res.dtype == "m8[us]"

def test_truediv_timedeltalike(self, td):
assert td / td == 1
assert (2.5 * td) / td == 2.5

other = Timedelta(td.value)
msg = "with mismatched resolutions are not supported"
with pytest.raises(ValueError, match=msg):
td / other

with pytest.raises(ValueError, match=msg):
# __rtruediv__
other.to_pytimedelta() / td

def test_truediv_numeric(self, td):
assert td / np.nan is NaT

res = td / 2
assert res.value == td.value / 2
assert res._reso == td._reso

res = td / 2.0
assert res.value == td.value / 2
assert res._reso == td._reso

def test_floordiv_timedeltalike(self, td):
assert td // td == 1
assert (2.5 * td) // td == 2

other = Timedelta(td.value)
msg = "with mismatched resolutions are not supported"
with pytest.raises(ValueError, match=msg):
td // other

with pytest.raises(ValueError, match=msg):
# __rfloordiv__
other.to_pytimedelta() // td

def test_floordiv_numeric(self, td):
assert td // np.nan is NaT

res = td // 2
assert res.value == td.value // 2
assert res._reso == td._reso

res = td // 2.0
assert res.value == td.value // 2
assert res._reso == td._reso

assert td // np.array(np.nan) is NaT

res = td // np.array(2)
assert res.value == td.value // 2
assert res._reso == td._reso

res = td // np.array(2.0)
assert res.value == td.value // 2
assert res._reso == td._reso


class TestTimedeltaUnaryOps:
def test_invert(self):
Expand Down