Skip to content
Open
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
6 changes: 6 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,12 @@ def _get_arithmetic_result_freq(self, other) -> BaseOffset | None:
# e.g. TestTimedelta64ArithmeticUnsorted::test_timedelta
# Day is unambiguously 24h
return self.freq
elif (
lib.is_np_dtype(self.dtype, "M")
and isinstance(other, Timestamp)
and isinstance(self.freq, Day)
):
return self.freq

return None

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/datetimes/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ def test_add_dti_day(self):
result = (dti + dti.freq)[:-1]
expected = dti[1:]
tm.assert_index_equal(result, expected)

def test_sub_timestamp_preserves_day_freq(self):
# GH#62094
dti = date_range("2021-01-01", periods=5, freq="D")
ts = Timestamp("2020-01-01")

result = dti - ts

# The one crucial assertion:
assert isinstance(result, TimedeltaIndex)
assert result.freq == dti.freq
Loading