Skip to content

Commit 0ecb65a

Browse files
mroeschkemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#45909: BUG: DateOffset(n) not defaulting to days
1 parent b1e9a91 commit 0ecb65a

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v1.4.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Fixed regressions
2424
- Regression in :meth:`~Index.join` with overlapping :class:`IntervalIndex` raising an ``InvalidIndexError`` (:issue:`45661`)
2525
- Regression when setting values with :meth:`Series.loc` raising with all ``False`` indexer and :class:`Series` on the right hand side (:issue:`45778`)
2626
- Regression in :func:`read_sql` with a DBAPI2 connection that is not an instance of ``sqlite3.Connection`` incorrectly requiring SQLAlchemy be installed (:issue:`45660`)
27-
-
27+
- Regression in :class:`DateOffset` when constructing with an integer argument with no keywords (e.g. ``pd.DateOffset(n)``) would behave like ``datetime.timedelta(days=0)`` (:issue:`45643`, :issue:`45890`)
2828

2929
.. ---------------------------------------------------------------------------
3030

pandas/_libs/tslibs/offsets.pyx

+6-1
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,12 @@ cdef _determine_offset(kwds):
333333
else:
334334
# sub-daily offset - use timedelta (tz-aware)
335335
offset = timedelta(**kwds_no_nanos)
336+
elif any(nano in kwds for nano in ('nanosecond', 'nanoseconds')):
337+
offset = timedelta(days=0)
336338
else:
337-
offset = timedelta(0)
339+
# GH 45643/45890: (historically) defaults to 1 day for non-nano
340+
# since datetime.timedelta doesn't handle nanoseconds
341+
offset = timedelta(days=1)
338342
return offset, use_relativedelta
339343

340344

@@ -1223,6 +1227,7 @@ class DateOffset(RelativeDeltaOffset, metaclass=OffsetMeta):
12231227
----------
12241228
n : int, default 1
12251229
The number of time periods the offset represents.
1230+
If specified without a temporal pattern, defaults to n days.
12261231
normalize : bool, default False
12271232
Whether to round the result of a DateOffset addition down to the
12281233
previous midnight.

pandas/tests/tseries/offsets/test_offsets.py

+10
Original file line numberDiff line numberDiff line change
@@ -854,3 +854,13 @@ def test_dateoffset_misc():
854854
oset.freqstr
855855

856856
assert not offsets.DateOffset(months=2) == 2
857+
858+
859+
@pytest.mark.parametrize("n", [-1, 1, 3])
860+
def test_construct_int_arg_no_kwargs_assumed_days(n):
861+
# GH 45890, 45643
862+
offset = DateOffset(n)
863+
assert offset._offset == timedelta(1)
864+
result = Timestamp(2022, 1, 2) + offset
865+
expected = Timestamp(2022, 1, 2 + n)
866+
assert result == expected

0 commit comments

Comments
 (0)