From 7e10d08b1075d218602ed9c31c3e1c8d53b41513 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Mon, 17 Nov 2025 23:53:03 +0100 Subject: [PATCH 1/2] raise TypeError in __mul__ --- pandas/_libs/tslibs/timedeltas.pyx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 8343d51dcb4f1..e43b2688eba51 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -83,6 +83,7 @@ from pandas._libs.tslibs.offsets import Day from pandas._libs.tslibs.util cimport ( is_array, + is_bool_object, is_float_object, is_integer_object, ) @@ -2311,6 +2312,13 @@ class Timedelta(_Timedelta): return self.__mul__(item) return other * self.to_timedelta64() + elif is_bool_object(other): + # GH#62316 + raise TypeError( + "Cannot multiply Timedelta by bools. " + "Explicitly cast to integers instead." + ) + return NotImplemented __rmul__ = __mul__ From 2e0959193be4a2a1d03f3f899011009cceae3bb7 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Fri, 21 Nov 2025 23:59:27 +0100 Subject: [PATCH 2/2] add test, fix typo --- pandas/_libs/tslibs/timedeltas.pyx | 4 ++-- pandas/tests/scalar/timedelta/test_arithmetic.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index e43b2688eba51..4d30415a396db 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -2315,8 +2315,8 @@ class Timedelta(_Timedelta): elif is_bool_object(other): # GH#62316 raise TypeError( - "Cannot multiply Timedelta by bools. " - "Explicitly cast to integers instead." + "Cannot multiply Timedelta by bool. " + "Explicitly cast to integer instead." ) return NotImplemented diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index 6f7f2a339d944..1f6ece9f3e8f1 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -970,6 +970,12 @@ def test_td_op_timedelta_timedeltalike_array(self, op, arr): with pytest.raises(TypeError, match=msg): op(arr, Timedelta("1D")) + def test_mul_bool_invalid(self): + # GH#62316 + msg = "Cannot multiply Timedelta by bool. Explicitly cast to integer" + with pytest.raises(TypeError, match=msg): + Timedelta("1 day") * True + class TestTimedeltaComparison: def test_compare_pytimedelta_bounds(self):