Skip to content

Commit

Permalink
ENH: Raise TypeError instead of RecursionError when multiplying DateO…
Browse files Browse the repository at this point in the history
…ffsets (#59442)
  • Loading branch information
mroeschke authored Aug 8, 2024
1 parent 93198fb commit 0cdc6a4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
6 changes: 6 additions & 0 deletions pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/tseries/offsets/test_offsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0cdc6a4

Please sign in to comment.