Skip to content

Commit 5ba3d62

Browse files
authored
BUG: td64 * masked_dtype raising (#62986)
1 parent 893c784 commit 5ba3d62

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ Timedelta
10171017
- Bug in :class:`Timedelta` constructor failing to raise when passed an invalid keyword (:issue:`53801`)
10181018
- Bug in :meth:`DataFrame.cumsum` which was raising ``IndexError`` if dtype is ``timedelta64[ns]`` (:issue:`57956`)
10191019
- Bug in multiplication operations with ``timedelta64`` dtype failing to raise ``TypeError`` when multiplying by ``bool`` objects or dtypes (:issue:`58054`)
1020+
- Bug in multiplication operations with ``timedelta64`` dtype incorrectly raising when multiplying by numpy-nullable dtypes or pyarrow integer dtypes (:issue:`58054`)
10201021

10211022
Timezones
10221023
^^^^^^^^^

pandas/core/arrays/timedeltas.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151
is_string_dtype,
5252
pandas_dtype,
5353
)
54-
from pandas.core.dtypes.dtypes import ExtensionDtype
54+
from pandas.core.dtypes.dtypes import (
55+
ArrowDtype,
56+
BaseMaskedDtype,
57+
ExtensionDtype,
58+
)
5559
from pandas.core.dtypes.missing import isna
5660

5761
from pandas.core import (
@@ -501,6 +505,10 @@ def __mul__(self, other) -> Self:
501505
f"Cannot multiply '{self.dtype}' by bool, explicitly cast to "
502506
"integers instead"
503507
)
508+
if isinstance(other.dtype, (ArrowDtype, BaseMaskedDtype)):
509+
# GH#58054
510+
return NotImplemented
511+
504512
if len(other) != len(self) and not lib.is_np_dtype(other.dtype, "m"):
505513
# Exclude timedelta64 here so we correctly raise TypeError
506514
# for that instead of ValueError

pandas/tests/arithmetic/test_timedelta64.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,31 @@ def test_td64arr_mul_bool_raises(self, dtype, box_with_array):
16021602
with pytest.raises(TypeError, match=msg2):
16031603
other * obj
16041604

1605+
@pytest.mark.parametrize(
1606+
"dtype",
1607+
[
1608+
"Int64",
1609+
"Float64",
1610+
pytest.param("int64[pyarrow]", marks=td.skip_if_no("pyarrow")),
1611+
],
1612+
)
1613+
def test_td64arr_mul_masked(self, dtype, box_with_array):
1614+
ser = Series(np.arange(5) * timedelta(hours=1))
1615+
obj = tm.box_expected(ser, box_with_array)
1616+
1617+
other = Series(np.arange(5), dtype=dtype)
1618+
other = tm.box_expected(other, box_with_array)
1619+
1620+
expected = Series([Timedelta(hours=n**2) for n in range(5)])
1621+
expected = tm.box_expected(expected, box_with_array)
1622+
if dtype == "int64[pyarrow]":
1623+
expected = expected.astype("duration[ns][pyarrow]")
1624+
1625+
result = obj * other
1626+
tm.assert_equal(result, expected)
1627+
result = other * obj
1628+
tm.assert_equal(result, expected)
1629+
16051630
# ------------------------------------------------------------------
16061631
# __div__, __rdiv__
16071632

0 commit comments

Comments
 (0)