Skip to content

BUG: DTI-datetime_scalar preserve freq #48818

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 3 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Datetimelike
^^^^^^^^^^^^
- Bug in :func:`pandas.infer_freq`, raising ``TypeError`` when inferred on :class:`RangeIndex` (:issue:`47084`)
- Bug in :class:`DatetimeIndex` constructor failing to raise when ``tz=None`` is explicitly specified in conjunction with timezone-aware ``dtype`` or data (:issue:`48659`)
- Bug in subtracting a ``datetime`` scalar from :class:`DatetimeIndex` failing to retain the original ``freq`` attribute (:issue:`48818`)

Timedelta
^^^^^^^^^
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,16 @@ def _sub_datetimelike_scalar(self, other: datetime | np.datetime64):

i8 = self.asi8
result = checked_add_with_arr(i8, -other.value, arr_mask=self._isnan)
return result.view("timedelta64[ns]")
res_m8 = result.view(f"timedelta64[{self._unit}]")

new_freq = None
if isinstance(self.freq, Tick):
# adding a scalar preserves freq
new_freq = self.freq

from pandas.core.arrays import TimedeltaArray

return TimedeltaArray._simple_new(res_m8, dtype=res_m8.dtype, freq=new_freq)

@final
def _sub_datetime_arraylike(self, other):
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@


class TestDatetimeIndex:
def test_sub_datetime_preserves_reso(self, tz_naive_fixture):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test where this goes over a DST transition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch. when we cross a DST transition, dti.freq is actually wrong bc of the (deprecated) kludge in generate_range. As a result, inheriting the freq is incorrect in this case. But it will return to being correct once the deprecation is enforced.

i think the thing to do is add the test and xfail it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test added (and xfailed) + green

# GH#48818
dti = date_range("2016-01-01", periods=12, tz=tz_naive_fixture)

res = dti - dti[0]
expected = pd.timedelta_range("0 Days", "11 Days")
tm.assert_index_equal(res, expected)
assert res.freq == expected.freq

def test_time_overflow_for_32bit_machines(self):
# GH8943. On some machines NumPy defaults to np.int32 (for example,
# 32-bit Linux machines). In the function _generate_regular_range
Expand Down