Skip to content

Commit 7a5a5c7

Browse files
authored
BUG: dt64 + DateOffset with milliseconds (pandas-dev#57536)
* BUG: dt64 + DateOffset with milliseconds * remove print
1 parent 33a1cd7 commit 7a5a5c7

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Fixed regressions
4242
- Fixed regression in :meth:`Series.astype` introducing decimals when converting from integer with missing values to string dtype (:issue:`57418`)
4343
- Fixed regression in :meth:`Series.pct_change` raising a ``ValueError`` for an empty :class:`Series` (:issue:`57056`)
4444
- Fixed regression in :meth:`Series.to_numpy` when dtype is given as float and the data contains NaNs (:issue:`57121`)
45+
- Fixed regression in addition or subtraction of :class:`DateOffset` objects with millisecond components to ``datetime64`` :class:`Index`, :class:`Series`, or :class:`DataFrame` (:issue:`57529`)
4546

4647
.. ---------------------------------------------------------------------------
4748
.. _whatsnew_221.bug_fixes:

pandas/_libs/tslibs/offsets.pyx

+14-1
Original file line numberDiff line numberDiff line change
@@ -1410,13 +1410,22 @@ cdef class RelativeDeltaOffset(BaseOffset):
14101410
"minutes",
14111411
"seconds",
14121412
"microseconds",
1413+
"milliseconds",
14131414
}
14141415
# relativedelta/_offset path only valid for base DateOffset
14151416
if self._use_relativedelta and set(kwds).issubset(relativedelta_fast):
1417+
td_args = {
1418+
"days",
1419+
"hours",
1420+
"minutes",
1421+
"seconds",
1422+
"microseconds",
1423+
"milliseconds"
1424+
}
14161425
td_kwds = {
14171426
key: val
14181427
for key, val in kwds.items()
1419-
if key in ["days", "hours", "minutes", "seconds", "microseconds"]
1428+
if key in td_args
14201429
}
14211430
if "weeks" in kwds:
14221431
days = td_kwds.get("days", 0)
@@ -1426,6 +1435,8 @@ cdef class RelativeDeltaOffset(BaseOffset):
14261435
delta = Timedelta(**td_kwds)
14271436
if "microseconds" in kwds:
14281437
delta = delta.as_unit("us")
1438+
elif "milliseconds" in kwds:
1439+
delta = delta.as_unit("ms")
14291440
else:
14301441
delta = delta.as_unit("s")
14311442
else:
@@ -1443,6 +1454,8 @@ cdef class RelativeDeltaOffset(BaseOffset):
14431454
delta = Timedelta(self._offset * self.n)
14441455
if "microseconds" in kwds:
14451456
delta = delta.as_unit("us")
1457+
elif "milliseconds" in kwds:
1458+
delta = delta.as_unit("ms")
14461459
else:
14471460
delta = delta.as_unit("s")
14481461
return delta

pandas/tests/arithmetic/test_datetime64.py

+32
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,38 @@ def test_dti_add_sub_nonzero_mth_offset(
15811581
expected = tm.box_expected(expected, box_with_array, False)
15821582
tm.assert_equal(result, expected)
15831583

1584+
def test_dt64arr_series_add_DateOffset_with_milli(self):
1585+
# GH 57529
1586+
dti = DatetimeIndex(
1587+
[
1588+
"2000-01-01 00:00:00.012345678",
1589+
"2000-01-31 00:00:00.012345678",
1590+
"2000-02-29 00:00:00.012345678",
1591+
],
1592+
dtype="datetime64[ns]",
1593+
)
1594+
result = dti + DateOffset(milliseconds=4)
1595+
expected = DatetimeIndex(
1596+
[
1597+
"2000-01-01 00:00:00.016345678",
1598+
"2000-01-31 00:00:00.016345678",
1599+
"2000-02-29 00:00:00.016345678",
1600+
],
1601+
dtype="datetime64[ns]",
1602+
)
1603+
tm.assert_index_equal(result, expected)
1604+
1605+
result = dti + DateOffset(days=1, milliseconds=4)
1606+
expected = DatetimeIndex(
1607+
[
1608+
"2000-01-02 00:00:00.016345678",
1609+
"2000-02-01 00:00:00.016345678",
1610+
"2000-03-01 00:00:00.016345678",
1611+
],
1612+
dtype="datetime64[ns]",
1613+
)
1614+
tm.assert_index_equal(result, expected)
1615+
15841616

15851617
class TestDatetime64OverflowHandling:
15861618
# TODO: box + de-duplicate

0 commit comments

Comments
 (0)