Skip to content

Commit 795cce2

Browse files
Backport PR #59441 on branch 2.2.x (COMPAT: Fix numpy 2.1 timedelta * DateOffset) (#59444)
Backport PR #59441: COMPAT: Fix numpy 2.1 timedelta * DateOffset Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 785880c commit 795cce2

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
@@ -468,6 +468,10 @@ def __mul__(self, other) -> Self:
468468
if is_scalar(other):
469469
# numpy will accept float and int, raise TypeError for others
470470
result = self._ndarray * other
471+
if result.dtype.kind != "m":
472+
# numpy >= 2.1 may not raise a TypeError
473+
# and seems to dispatch to others.__rmul__?
474+
raise TypeError(f"Cannot multiply with {type(other).__name__}")
471475
freq = None
472476
if self.freq is not None and not isna(other):
473477
freq = self.freq * other
@@ -495,6 +499,10 @@ def __mul__(self, other) -> Self:
495499

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

500508
__rmul__ = __mul__

pandas/tests/arithmetic/test_timedelta64.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,13 @@ def test_td64arr_mul_int(self, box_with_array):
14541454
def test_td64arr_mul_tdlike_scalar_raises(self, two_hours, box_with_array):
14551455
rng = timedelta_range("1 days", "10 days", name="foo")
14561456
rng = tm.box_expected(rng, box_with_array)
1457-
msg = "argument must be an integer|cannot use operands with types dtype"
1457+
msg = "|".join(
1458+
[
1459+
"argument must be an integer",
1460+
"cannot use operands with types dtype",
1461+
"Cannot multiply with",
1462+
]
1463+
)
14581464
with pytest.raises(TypeError, match=msg):
14591465
rng * two_hours
14601466

0 commit comments

Comments
 (0)