Skip to content

Commit bc75a72

Browse files
makbigcjreback
authored andcommitted
[BUG] Adding offset with nonzero month to DatetimeIndex (GH26258) (#26292)
1 parent 4a30fa5 commit bc75a72

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ Datetimelike
276276
- Improved :class:`Timestamp` type checking in various datetime functions to prevent exceptions when using a subclassed ``datetime`` (:issue:`25851`)
277277
- Bug in :class:`Series` and :class:`DataFrame` repr where ``np.datetime64('NaT')`` and ``np.timedelta64('NaT')`` with ``dtype=object`` would be represented as ``NaN`` (:issue:`25445`)
278278
- Bug in :func:`to_datetime` which does not replace the invalid argument with ``NaT`` when error is set to coerce (:issue:`26122`)
279+
- Bug in adding :class:`DateOffset` with nonzero month to :class:`DatetimeIndex` would raise ``ValueError`` (:issue:`26258`)
279280

280281
Timedelta
281282
^^^^^^^^^

pandas/tests/arithmetic/test_datetime64.py

+22
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,28 @@ def test_dt64arr_add_sub_offset_ndarray(self, tz_naive_fixture,
14351435
expected = tm.box_expected(expected, box_with_array)
14361436
tm.assert_equal(res, expected)
14371437

1438+
@pytest.mark.parametrize("op, offset, exp", [
1439+
('__add__', pd.DateOffset(months=3, days=10),
1440+
DatetimeIndex([Timestamp('2014-04-11'), Timestamp('2015-04-11'),
1441+
Timestamp('2016-04-11'), Timestamp('2017-04-11')])),
1442+
('__add__', pd.DateOffset(months=3),
1443+
DatetimeIndex([Timestamp('2014-04-01'), Timestamp('2015-04-01'),
1444+
Timestamp('2016-04-01'), Timestamp('2017-04-01')])),
1445+
('__sub__', pd.DateOffset(months=3, days=10),
1446+
DatetimeIndex([Timestamp('2013-09-21'), Timestamp('2014-09-21'),
1447+
Timestamp('2015-09-21'), Timestamp('2016-09-21')])),
1448+
('__sub__', pd.DateOffset(months=3),
1449+
DatetimeIndex([Timestamp('2013-10-01'), Timestamp('2014-10-01'),
1450+
Timestamp('2015-10-01'), Timestamp('2016-10-01')]))
1451+
1452+
])
1453+
def test_dti_add_sub_nonzero_mth_offset(self, op, offset, exp):
1454+
# GH 26258
1455+
date = date_range(start='01 Jan 2014', end='01 Jan 2017', freq='AS')
1456+
mth = getattr(date, op)
1457+
result = mth(offset)
1458+
tm.assert_equal(result, exp)
1459+
14381460

14391461
class TestDatetime64OverflowHandling:
14401462
# TODO: box + de-duplicate

pandas/tseries/offsets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def apply_index(self, i):
273273
kwds.get('months', 0)) * self.n)
274274
if months:
275275
shifted = liboffsets.shift_months(i.asi8, months)
276-
i = type(i)(shifted, freq=i.freq, dtype=i.dtype)
276+
i = type(i)(shifted, dtype=i.dtype)
277277

278278
weeks = (kwds.get('weeks', 0)) * self.n
279279
if weeks:

0 commit comments

Comments
 (0)