Skip to content

BUG: adding Timedelta to DatetimeIndex raising incorrectly #37780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2020
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- Regression in addition of a timedelta-like scalar to a :class:`DatetimeIndex` raising incorrectly (:issue:`37295`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ def _add_timedeltalike_scalar(self, other):
# adding a scalar preserves freq
new_freq = self.freq

return type(self)(new_values, dtype=self.dtype, freq=new_freq)
return type(self)._simple_new(new_values, dtype=self.dtype, freq=new_freq)

def _add_timedelta_arraylike(self, other):
"""
Expand Down
14 changes: 13 additions & 1 deletion pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

import pandas as pd
from pandas import DatetimeIndex, Index, Timestamp, date_range, offsets
from pandas import DatetimeIndex, Index, Timedelta, Timestamp, date_range, offsets
import pandas._testing as tm


Expand Down Expand Up @@ -408,3 +408,15 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
dtype="UInt32",
)
tm.assert_frame_equal(result, expected_data_frame)


def test_add_timedelta_preserves_freq():
# GH#37295 should hold for any DTI with freq=None or Tick freq
tz = "Canada/Eastern"
dti = date_range(
start=Timestamp("2019-03-26 00:00:00-0400", tz=tz),
end=Timestamp("2020-10-17 00:00:00-0400", tz=tz),
freq="D",
)
result = dti + Timedelta(days=1)
assert result.freq == dti.freq