Skip to content

Commit 6d1eca5

Browse files
committed
DOC: note fix for DatetimeIndex subtraction GH#62094
1 parent bb10b27 commit 6d1eca5

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,10 @@ Datetimelike
758758
- Bug in :meth:`to_datetime` reports incorrect index in case of any failure scenario. (:issue:`58298`)
759759
- Bug in :meth:`to_datetime` with ``format="ISO8601"`` and ``utc=True`` where naive timestamps incorrectly inherited timezone offset from previous timestamps in a series. (:issue:`61389`)
760760
- Bug in :meth:`to_datetime` wrongly converts when ``arg`` is a ``np.datetime64`` object with unit of ``ps``. (:issue:`60341`)
761+
- Bug in subtracting a :class:`Timestamp` from a :class:`DatetimeIndex` with
762+
``freq="D"`` dropping the frequency and causing subsequent
763+
:meth:`TimedeltaIndex.shift` calls to raise ``NullFrequencyError``
764+
(:issue:`62094`)
761765
- Bug in constructing arrays with :class:`ArrowDtype` with ``timestamp`` type incorrectly allowing ``Decimal("NaN")`` (:issue:`61773`)
762766
- Bug in constructing arrays with a timezone-aware :class:`ArrowDtype` from timezone-naive datetime objects incorrectly treating those as UTC times instead of wall times like :class:`DatetimeTZDtype` (:issue:`61775`)
763767
- Bug in setting scalar values with mismatched resolution into arrays with non-nanosecond ``datetime64``, ``timedelta64`` or :class:`DatetimeTZDtype` incorrectly truncating those scalars (:issue:`56410`)

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)