Skip to content

Commit 0cdc6a4

Browse files
authored
ENH: Raise TypeError instead of RecursionError when multiplying DateOffsets (#59442)
1 parent 93198fb commit 0cdc6a4

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Other enhancements
5050
- :meth:`DataFrame.pivot_table` and :func:`pivot_table` now allow the passing of keyword arguments to ``aggfunc`` through ``**kwargs`` (:issue:`57884`)
5151
- :meth:`Series.cummin` and :meth:`Series.cummax` now supports :class:`CategoricalDtype` (:issue:`52335`)
5252
- :meth:`Series.plot` now correctly handle the ``ylabel`` parameter for pie charts, allowing for explicit control over the y-axis label (:issue:`58239`)
53+
- Multiplying two :class:`DateOffset` objects will now raise a ``TypeError`` instead of a ``RecursionError`` (:issue:`59442`)
5354
- Restore support for reading Stata 104-format and enable reading 103-format dta files (:issue:`58554`)
5455
- Support reading Stata 102-format (Stata 1) dta files (:issue:`58978`)
5556
- Support reading Stata 110-format (Stata 7) dta files (:issue:`47176`)

pandas/_libs/tslibs/offsets.pyx

+6
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,12 @@ cdef class BaseOffset:
491491
elif is_integer_object(other):
492492
return type(self)(n=other * self.n, normalize=self.normalize,
493493
**self.kwds)
494+
elif isinstance(other, BaseOffset):
495+
# Otherwise raises RecurrsionError due to __rmul__
496+
raise TypeError(
497+
f"Cannot multiply {type(self).__name__} with "
498+
f"{type(other).__name__}."
499+
)
494500
return NotImplemented
495501

496502
def __rmul__(self, other):

pandas/tests/tseries/offsets/test_offsets.py

+7
Original file line numberDiff line numberDiff line change
@@ -1182,3 +1182,10 @@ def test_is_yqm_start_end():
11821182

11831183
for ts, value in tests:
11841184
assert ts == value
1185+
1186+
1187+
@pytest.mark.parametrize("left", [DateOffset(1), Nano(1)])
1188+
@pytest.mark.parametrize("right", [DateOffset(1), Nano(1)])
1189+
def test_multiply_dateoffset_typeerror(left, right):
1190+
with pytest.raises(TypeError, match="Cannot multiply"):
1191+
left * right

0 commit comments

Comments
 (0)