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 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
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
30 changes: 30 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,36 @@


class TestDatetimeIndex:
def test_sub_datetime_preserves_freq(self, tz_naive_fixture):
# 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

@pytest.mark.xfail(
reason="The inherited freq is incorrect bc dti.freq is incorrect "
"https://github.com/pandas-dev/pandas/pull/48818/files#r982793461"
)
def test_sub_datetime_preserves_freq_across_dst(self):
# GH#48818
ts = Timestamp("2016-03-11", tz="US/Pacific")
dti = date_range(ts, periods=4)

res = dti - dti[0]
expected = pd.TimedeltaIndex(
[
pd.Timedelta(days=0),
pd.Timedelta(days=1),
pd.Timedelta(days=2),
pd.Timedelta(days=2, hours=23),
]
)
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