Skip to content

Backport PR #59441 on branch 2.2.x (COMPAT: Fix numpy 2.1 timedelta * DateOffset) #59444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading