Skip to content

Commit

Permalink
Backport PR pandas-dev#59441: COMPAT: Fix numpy 2.1 timedelta * DateO…
Browse files Browse the repository at this point in the history
…ffset
  • Loading branch information
mroeschke authored and meeseeksmachine committed Aug 7, 2024
1 parent 785880c commit 7f2fd6b
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 7f2fd6b

Please sign in to comment.