Skip to content

Commit 08bf822

Browse files
authored
BUG: adding Timedelta to DatetimeIndex raising incorrectly (#37780)
1 parent e35bfd5 commit 08bf822

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

doc/source/whatsnew/v1.1.5.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17-
-
17+
- Regression in addition of a timedelta-like scalar to a :class:`DatetimeIndex` raising incorrectly (:issue:`37295`)
1818
-
1919

2020
.. ---------------------------------------------------------------------------

pandas/core/arrays/datetimelike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ def _add_timedeltalike_scalar(self, other):
964964
# adding a scalar preserves freq
965965
new_freq = self.freq
966966

967-
return type(self)(new_values, dtype=self.dtype, freq=new_freq)
967+
return type(self)._simple_new(new_values, dtype=self.dtype, freq=new_freq)
968968

969969
def _add_timedelta_arraylike(self, other):
970970
"""

pandas/tests/indexes/datetimes/test_misc.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
import pandas as pd
10-
from pandas import DatetimeIndex, Index, Timestamp, date_range, offsets
10+
from pandas import DatetimeIndex, Index, Timedelta, Timestamp, date_range, offsets
1111
import pandas._testing as tm
1212

1313

@@ -408,3 +408,15 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
408408
dtype="UInt32",
409409
)
410410
tm.assert_frame_equal(result, expected_data_frame)
411+
412+
413+
def test_add_timedelta_preserves_freq():
414+
# GH#37295 should hold for any DTI with freq=None or Tick freq
415+
tz = "Canada/Eastern"
416+
dti = date_range(
417+
start=Timestamp("2019-03-26 00:00:00-0400", tz=tz),
418+
end=Timestamp("2020-10-17 00:00:00-0400", tz=tz),
419+
freq="D",
420+
)
421+
result = dti + Timedelta(days=1)
422+
assert result.freq == dti.freq

0 commit comments

Comments
 (0)