diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 32c98fbf9d655..3de65fe6f682c 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -50,6 +50,7 @@ Other enhancements - :meth:`DataFrame.pivot_table` and :func:`pivot_table` now allow the passing of keyword arguments to ``aggfunc`` through ``**kwargs`` (:issue:`57884`) - :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`) - :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`) +- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`) - Restore support for reading Stata 104-format and enable reading 103-format dta files (:issue:`58554`) - Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`) - Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`) diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index fd1bb3fe3e173..554c4f109f1c5 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -491,6 +491,12 @@ cdef class BaseOffset: elif is_integer_object(other): return type(self)(n=other * self.n, normalize=self.normalize, **self.kwds) + elif isinstance(other, BaseOffset): + # Otherwise raises RecurrsionError due to __rmul__ + raise TypeError( + f"Cannot multiply {type(self).__name__} with " + f"{type(other).__name__}." + ) return NotImplemented def __rmul__(self, other): diff --git a/pandas/tests/tseries/offsets/test_offsets.py b/pandas/tests/tseries/offsets/test_offsets.py index d19717e87c7d2..d0192c12f9518 100644 --- a/pandas/tests/tseries/offsets/test_offsets.py +++ b/pandas/tests/tseries/offsets/test_offsets.py @@ -1182,3 +1182,10 @@ def test_is_yqm_start_end(): for ts, value in tests: assert ts == value + + +@pytest.mark.parametrize("left", [DateOffset(1), Nano(1)]) +@pytest.mark.parametrize("right", [DateOffset(1), Nano(1)]) +def test_multiply_dateoffset_typeerror(left, right): + with pytest.raises(TypeError, match="Cannot multiply"): + left * right