Skip to content

BUG: inconsistent behavior of DateOffset #47953 #53681

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 14 commits into from
Jun 23, 2023
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ Categorical
Datetimelike
^^^^^^^^^^^^
- :meth:`DatetimeIndex.map` with ``na_action="ignore"`` now works as expected. (:issue:`51644`)
- Bug in :class:`DateOffset` which had inconsistent behavior when multiplying a :class:`DateOffset` object by a constant (:issue:`47953`)
- Bug in :func:`date_range` when ``freq`` was a :class:`DateOffset` with ``nanoseconds`` (:issue:`46877`)
- Bug in :meth:`Timestamp.date`, :meth:`Timestamp.isocalendar`, :meth:`Timestamp.timetuple`, and :meth:`Timestamp.toordinal` were returning incorrect results for inputs outside those supported by the Python standard library's datetime module (:issue:`53668`)
- Bug in :meth:`Timestamp.round` with values close to the implementation bounds returning incorrect results instead of raising ``OutOfBoundsDatetime`` (:issue:`51494`)
Expand Down
7 changes: 1 addition & 6 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1221,12 +1221,7 @@ cdef class RelativeDeltaOffset(BaseOffset):
# perform calculation in UTC
other = other.replace(tzinfo=None)

if self.n > 0:
for i in range(self.n):
other = other + self._offset
else:
for i in range(-self.n):
other = other - self._offset
other = other + (self._offset * self.n)

if hasattr(self, "nanoseconds"):
other = self.n * Timedelta(nanoseconds=self.nanoseconds) + other
Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from pandas.errors import PerformanceWarning

from pandas import (
DataFrame,
DatetimeIndex,
Series,
date_range,
Expand Down Expand Up @@ -1067,3 +1068,51 @@ def test_dateoffset_add_sub_timestamp_series_with_nano(offset, expected):
assert testseries[0] == teststamp
testseries = offset + testseries
assert testseries[0] == expected


@pytest.mark.parametrize(
"n_months, scaling_factor, start_timestamp, expected_timestamp",
[
(1, 2, "2020-01-30", "2020-03-30"),
(2, 1, "2020-01-30", "2020-03-30"),
(1, 0, "2020-01-30", "2020-01-30"),
(2, 0, "2020-01-30", "2020-01-30"),
(1, -1, "2020-01-30", "2019-12-30"),
(2, -1, "2020-01-30", "2019-11-30"),
],
)
def test_offset_multiplication(
n_months, scaling_factor, start_timestamp, expected_timestamp
):
# GH 47953
mo1 = DateOffset(months=n_months)

startscalar = Timestamp(start_timestamp)
startarray = Series([startscalar])

resultscalar = startscalar + (mo1 * scaling_factor)
resultarray = startarray + (mo1 * scaling_factor)

expectedscalar = Timestamp(expected_timestamp)
expectedarray = Series([expectedscalar])
assert resultscalar == expectedscalar

tm.assert_series_equal(resultarray, expectedarray)


def test_dateoffset_operations_on_dataframes():
# GH 47953
df = DataFrame({"T": [Timestamp("2019-04-30")], "D": [DateOffset(months=1)]})
frameresult1 = df["T"] + 26 * df["D"]
df2 = DataFrame(
{
"T": [Timestamp("2019-04-30"), Timestamp("2019-04-30")],
"D": [DateOffset(months=1), DateOffset(months=1)],
}
)
expecteddate = Timestamp("2021-06-30")
with tm.assert_produces_warning(PerformanceWarning):
frameresult2 = df2["T"] + 26 * df2["D"]

assert frameresult1[0] == expecteddate
assert frameresult2[0] == expecteddate