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.0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
- Bug in :class:`DateOffset` which had inconsistent behavior when multiplying a :class:`DateOffset` object by a constant (:issue:`47953`)
- Bug in :func:`RangeIndex.union` when using ``sort=True`` with another :class:`RangeIndex` (:issue:`53490`)
- Bug in :func:`read_csv` when defining ``dtype`` with ``bool[pyarrow]`` for the ``"c"`` and ``"python"`` engines (:issue:`53390`)
- Bug in :meth:`Series.str.split` and :meth:`Series.str.rsplit` with ``expand=True`` for :class:`ArrowDtype` with ``pyarrow.string`` (:issue:`53532`)
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
42 changes: 42 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pandas.errors import PerformanceWarning

from pandas import (
DataFrame,
DatetimeIndex,
Series,
date_range,
Expand Down Expand Up @@ -1075,3 +1076,44 @@ 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")],
)
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