Skip to content

Commit d0cb205

Browse files
authored
COMPAT: Fix numpy 2.1 timedelta * DateOffset (#59441)
1 parent f9232ff commit d0cb205

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pandas/core/arrays/timedeltas.py

+8
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ def __mul__(self, other) -> Self:
467467
if is_scalar(other):
468468
# numpy will accept float and int, raise TypeError for others
469469
result = self._ndarray * other
470+
if result.dtype.kind != "m":
471+
# numpy >= 2.1 may not raise a TypeError
472+
# and seems to dispatch to others.__rmul__?
473+
raise TypeError(f"Cannot multiply with {type(other).__name__}")
470474
freq = None
471475
if self.freq is not None and not isna(other):
472476
freq = self.freq * other
@@ -494,6 +498,10 @@ def __mul__(self, other) -> Self:
494498

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

499507
__rmul__ = __mul__

pandas/tests/arithmetic/test_timedelta64.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,13 @@ def test_td64arr_mul_int(self, box_with_array):
14601460
def test_td64arr_mul_tdlike_scalar_raises(self, two_hours, box_with_array):
14611461
rng = timedelta_range("1 days", "10 days", name="foo")
14621462
rng = tm.box_expected(rng, box_with_array)
1463-
msg = "argument must be an integer|cannot use operands with types dtype"
1463+
msg = "|".join(
1464+
[
1465+
"argument must be an integer",
1466+
"cannot use operands with types dtype",
1467+
"Cannot multiply with",
1468+
]
1469+
)
14641470
with pytest.raises(TypeError, match=msg):
14651471
rng * two_hours
14661472

0 commit comments

Comments
 (0)