Skip to content

Commit

Permalink
Backport PR #59441 on branch 2.2.x (COMPAT: Fix numpy 2.1 timedelta *…
Browse files Browse the repository at this point in the history
… DateOffset) (#59444)

Backport PR #59441: COMPAT: Fix numpy 2.1 timedelta * DateOffset

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and mroeschke authored Aug 7, 2024
1 parent 785880c commit 795cce2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,10 @@ def __mul__(self, other) -> Self:
if is_scalar(other):
# numpy will accept float and int, raise TypeError for others
result = self._ndarray * other
if result.dtype.kind != "m":
# numpy >= 2.1 may not raise a TypeError
# and seems to dispatch to others.__rmul__?
raise TypeError(f"Cannot multiply with {type(other).__name__}")
freq = None
if self.freq is not None and not isna(other):
freq = self.freq * other
Expand Down Expand Up @@ -495,6 +499,10 @@ def __mul__(self, other) -> Self:

# numpy will accept float or int dtype, raise TypeError for others
result = self._ndarray * other
if result.dtype.kind != "m":
# numpy >= 2.1 may not raise a TypeError
# and seems to dispatch to others.__rmul__?
raise TypeError(f"Cannot multiply with {type(other).__name__}")
return type(self)._simple_new(result, dtype=result.dtype)

__rmul__ = __mul__
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/arithmetic/test_timedelta64.py
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,13 @@ def test_td64arr_mul_int(self, box_with_array):
def test_td64arr_mul_tdlike_scalar_raises(self, two_hours, box_with_array):
rng = timedelta_range("1 days", "10 days", name="foo")
rng = tm.box_expected(rng, box_with_array)
msg = "argument must be an integer|cannot use operands with types dtype"
msg = "|".join(
[
"argument must be an integer",
"cannot use operands with types dtype",
"Cannot multiply with",
]
)
with pytest.raises(TypeError, match=msg):
rng * two_hours

Expand Down

0 comments on commit 795cce2

Please sign in to comment.