Skip to content

BUG: TDI divison giving bogus .freq #51575

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 4 commits into from
Feb 25, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Datetimelike
Timedelta
^^^^^^^^^
- Bug in :meth:`Timedelta.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsTimedelta`` (:issue:`51494`)
- Bug in :class:`TimedeltaIndex` division or multiplication leading to ``.freq`` of "0 Days" instead of ``None`` (:issue:`51575`)
-

Timezones
Expand Down
18 changes: 7 additions & 11 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,9 @@ def __mul__(self, other) -> TimedeltaArray:
freq = None
if self.freq is not None and not isna(other):
freq = self.freq * other
if freq.n == 0:
# GH#51575 Better to have no freq than an incorrect one
freq = None
return type(self)._simple_new(result, dtype=result.dtype, freq=freq)

if not hasattr(other, "dtype"):
Expand Down Expand Up @@ -526,17 +529,10 @@ def _scalar_divlike_op(self, other, op):
# Note: freq gets division, not floor-division, even if op
# is floordiv.
freq = self.freq / other

# TODO: 2022-12-24 test_ufunc_coercions, test_tdi_ops_attributes
# get here for truediv, no tests for floordiv

if op is operator.floordiv:
if freq.nanos == 0 and self.freq.nanos != 0:
# e.g. if self.freq is Nano(1) then dividing by 2
# rounds down to zero
# TODO: 2022-12-24 should implement the same check
# for truediv case
freq = None
if freq.nanos == 0 and self.freq.nanos != 0:
# e.g. if self.freq is Nano(1) then dividing by 2
# rounds down to zero
freq = None

return type(self)._simple_new(result, dtype=result.dtype, freq=freq)

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/indexes/timedeltas/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,21 @@ def test_freq_conversion(self, index_or_series):
assert expected.dtype == "m8[s]"
result = td.astype("timedelta64[s]")
tm.assert_equal(result, expected)

def test_arithmetic_zero_freq(self):
# GH#51575 don't get a .freq with freq.n = 0
tdi = timedelta_range(0, periods=100, freq="ns")
result = tdi / 2
assert result.freq is None
expected = tdi[:50].repeat(2)
tm.assert_index_equal(result, expected)

result2 = tdi // 2
assert result2.freq is None
expected2 = expected
tm.assert_index_equal(result2, expected2)

result3 = tdi * 0
assert result3.freq is None
expected3 = tdi[:1].repeat(100)
tm.assert_index_equal(result3, expected3)