diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index cd1cb0b64f74a..e8fdaf0ae5d49 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -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 ^^^^^^^^^ diff --git a/pandas/tests/tseries/offsets/test_yqm_offsets.py b/pandas/tests/tseries/offsets/test_yqm_offsets.py index 79a0e0f2c25eb..13cab9be46d37 100644 --- a/pandas/tests/tseries/offsets/test_yqm_offsets.py +++ b/pandas/tests/tseries/offsets/test_yqm_offsets.py @@ -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 diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index 8ba10f56f163c..b419d5ccc10b4 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -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): @@ -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): @@ -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):