Skip to content

Commit b69fb3c

Browse files
authored
ENH: clarify error message when multiplying bool times Timedelta (#63139)
1 parent 56b2089 commit b69fb3c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ from pandas._libs.tslibs.offsets import Day
8383

8484
from pandas._libs.tslibs.util cimport (
8585
is_array,
86+
is_bool_object,
8687
is_float_object,
8788
is_integer_object,
8889
)
@@ -2311,6 +2312,13 @@ class Timedelta(_Timedelta):
23112312
return self.__mul__(item)
23122313
return other * self.to_timedelta64()
23132314

2315+
elif is_bool_object(other):
2316+
# GH#62316
2317+
raise TypeError(
2318+
"Cannot multiply Timedelta by bool. "
2319+
"Explicitly cast to integer instead."
2320+
)
2321+
23142322
return NotImplemented
23152323

23162324
__rmul__ = __mul__

pandas/tests/scalar/timedelta/test_arithmetic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,12 @@ def test_td_op_timedelta_timedeltalike_array(self, op, arr):
970970
with pytest.raises(TypeError, match=msg):
971971
op(arr, Timedelta("1D"))
972972

973+
def test_mul_bool_invalid(self):
974+
# GH#62316
975+
msg = "Cannot multiply Timedelta by bool. Explicitly cast to integer"
976+
with pytest.raises(TypeError, match=msg):
977+
Timedelta("1 day") * True
978+
973979

974980
class TestTimedeltaComparison:
975981
def test_compare_pytimedelta_bounds(self):

0 commit comments

Comments
 (0)