Skip to content

Commit 72e0de9

Browse files
authored
Merge pull request #1 from akshat62/codex/fix-bug-related-to-issue-62094
Preserve day freq on DatetimeIndex subtraction
2 parents bb10b27 + 8943c27 commit 72e0de9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

pandas/core/arrays/datetimelike.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,16 @@ 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(self.freq, Day)
1095+
and isinstance(other, Timestamp)
1096+
and (self.tz is None or timezones.is_utc(self.tz))
1097+
and (other.tz is None or timezones.is_utc(other.tz))
1098+
):
1099+
# e.g. issue gh-62094: subtracting a Timestamp from a DTI
1100+
# with Day freq retains that freq
1101+
return self.freq
10921102

10931103
return None
10941104

pandas/tests/indexes/timedeltas/methods/test_shift.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,10 @@ def test_shift_no_freq(self):
7474
tdi = TimedeltaIndex(["1 days 01:00:00", "2 days 01:00:00"], freq=None)
7575
with pytest.raises(NullFrequencyError, match="Cannot shift with no freq"):
7676
tdi.shift(2)
77+
78+
def test_shift_after_dti_sub_timestamp(self):
79+
dti = pd.date_range("1/1/2021", "1/5/2021")
80+
tdi = dti - pd.Timestamp("1/3/2019")
81+
result = tdi.shift(1)
82+
expected = tdi + pd.Timedelta(days=1)
83+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)