Skip to content

Commit 09a4702

Browse files
committed
Preserve freq in TimedeltaIndex subtraction
1 parent 3e1d6d5 commit 09a4702

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,12 @@ def _get_arithmetic_result_freq(self, other) -> BaseOffset | None:
10891089
# e.g. TestTimedelta64ArithmeticUnsorted::test_timedelta
10901090
# Day is unambiguously 24h
10911091
return self.freq
1092+
elif (
1093+
lib.is_np_dtype(self.dtype, "M")
1094+
and isinstance(other, Timestamp)
1095+
and isinstance(self.freq, Day)
1096+
):
1097+
return self.freq
10921098

10931099
return None
10941100

pandas/tests/indexes/datetimes/test_arithmetic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,14 @@ def test_add_dti_day(self):
6363
result = (dti + dti.freq)[:-1]
6464
expected = dti[1:]
6565
tm.assert_index_equal(result, expected)
66+
67+
def test_sub_timestamp_preserves_day_freq(self):
68+
# GH#62094
69+
dti = date_range("2021-01-01", periods=5, freq="D")
70+
ts = Timestamp("2020-01-01")
71+
72+
result = dti - ts
73+
74+
# The one crucial assertion:
75+
assert isinstance(result, TimedeltaIndex)
76+
assert result.freq == dti.freq

0 commit comments

Comments
 (0)