Skip to content

BUG: freq not retained on apply_index #33779

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
Apr 24, 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ Datetimelike
- Bug in :meth:`DatetimeIndex.to_period` not infering the frequency when called with no arguments (:issue:`33358`)
- Bug in :meth:`DatetimeIndex.tz_localize` incorrectly retaining ``freq`` in some cases where the original freq is no longer valid (:issue:`30511`)
- Bug in :meth:`DatetimeIndex.intersection` losing ``freq`` and timezone in some cases (:issue:`33604`)
- Bug in :class:`DatetimeIndex` addition and subtraction with some types of :class:`DateOffset` objects incorrectly retaining an invalid ``freq`` attribute (:issue:`33779`)

Timedelta
^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/tseries/offsets/test_yqm_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def test_apply_index(cls, n):
ser = pd.Series(rng)

res = rng + offset
assert res.freq is None # not retained
res_v2 = offset.apply_index(rng)
assert (res == res_v2).all()
assert res[0] == rng[0] + offset
Expand Down
16 changes: 3 additions & 13 deletions pandas/tseries/offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,9 +1147,7 @@ def apply(self, other):
@apply_index_wraps
def apply_index(self, i):
shifted = liboffsets.shift_months(i.asi8, self.n, self._day_opt)
# TODO: going through __new__ raises on call to _validate_frequency;
# are we passing incorrect freq?
return type(i)._simple_new(shifted, freq=i.freq, dtype=i.dtype)
return type(i)._simple_new(shifted, dtype=i.dtype)


class MonthEnd(MonthOffset):
Expand Down Expand Up @@ -1868,11 +1866,7 @@ def apply_index(self, dtindex):
shifted = liboffsets.shift_quarters(
dtindex.asi8, self.n, self.startingMonth, self._day_opt
)
# TODO: going through __new__ raises on call to _validate_frequency;
# are we passing incorrect freq?
return type(dtindex)._simple_new(
shifted, freq=dtindex.freq, dtype=dtindex.dtype
)
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)


class BQuarterEnd(QuarterOffset):
Expand Down Expand Up @@ -1954,11 +1948,7 @@ def apply_index(self, dtindex):
shifted = liboffsets.shift_quarters(
dtindex.asi8, self.n, self.month, self._day_opt, modby=12
)
# TODO: going through __new__ raises on call to _validate_frequency;
# are we passing incorrect freq?
return type(dtindex)._simple_new(
shifted, freq=dtindex.freq, dtype=dtindex.dtype
)
return type(dtindex)._simple_new(shifted, dtype=dtindex.dtype)

def is_on_offset(self, dt: datetime) -> bool:
if self.normalize and not _is_normalized(dt):
Expand Down