{"record":{"id":"e75367a212290305","repo":"pandas-dev/pandas","slug":"cannot-multiply-self-dtype-by-bool-explicitly","errorCode":null,"errorMessage":"Cannot multiply '{self.dtype}' by bool, explicitly cast to integers instead","messagePattern":"Cannot multiply '(.+?)' by bool, explicitly cast to integers instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":519,"sourceCode":"            i8_result[nat_out] = iNaT\n        result = i8_result.view(self._ndarray.dtype)\n        return type(self)._simple_new(result, dtype=result.dtype)\n\n    def _mul_int_overflowsafe(self, i8_other: npt.NDArray[np.int64]) -> Self:\n        # GH#43178: mul_overflowsafe raises the low-level OverflowError; surface\n        #  it as OutOfBoundsTimedelta to match pandas' other td64 overflow paths.\n        try:\n            i8_result = mul_overflowsafe(self.asi8, i8_other)\n        except OverflowError as err:\n            raise OutOfBoundsTimedelta(\"Overflow in int64 multiplication\") from err\n        result = i8_result.view(self._ndarray.dtype)\n        return type(self)._simple_new(result, dtype=result.dtype)\n\n    @unpack_zerodim_and_defer(\"__mul__\")\n    def __mul__(self, other) -> Self:\n        if is_scalar(other):\n            if lib.is_bool(other):\n                raise TypeError(\n                    f\"Cannot multiply '{self.dtype}' by bool, explicitly cast to \"\n                    \"integers instead\"\n                )\n            other = _exact_if_integral(other)\n            if lib.is_integer(other):\n                # GH#43178: detect int64 overflow rather than silently wrapping\n                #  in the i8 cast below (e.g. a multiplier outside int64 bounds).\n                # TODO(numpy>=2.5): numpy detects this natively (numpy GH-31378)\n                #  but raises OverflowError; once the numpy floor is >= 2.5, drop\n                #  mul_overflowsafe and re-wrap numpy's error as\n                #  OutOfBoundsTimedelta. The non-integral float path isn't\n                #  covered and stays.\n                other = int(other)\n                if other > lib.i8max or other < -lib.i8max - 1:\n                    raise OutOfBoundsTimedelta(\"Overflow in int64 multiplication\")\n                i8_vals = self.asi8\n                if other != 0 and i8_vals.size:\n                    # The extreme elements bound all products, so checking them","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L501-L537","documentation":"Raised in TimedeltaArray.__mul__ when the scalar operand is a Python/numpy bool. Because multiplying a duration by a boolean is ambiguous (and numpy's silent true->1 promotion was deemed error-prone), pandas explicitly forbids it and directs the caller to cast the bool to integers first. This is the scalar entry; the array-bool case has its own message (GH#58054).","triggerScenarios":"`pd.Timedelta('1D') * True`, `pd.to_timedelta([1, 2], unit='D') * np.bool_(True)`, or passing a boolean mask/scalar where an integer was intended.","commonSituations":"Using a boolean condition or mask result directly as a timedelta multiplier; passing a `np.bool_` from a comparison into arithmetic.","solutions":["Cast the bool to int explicitly: `int(my_bool)` or `.astype(int)` for arrays.","Use a genuine integer/float scalar if a multiplier was intended."],"exampleFix":"# before\npd.to_timedelta([1, 2], unit='D') * True  # TypeError\n\n# after\npd.to_timedelta([1, 2], unit='D') * int(True)","handlingStrategy":"type-guard","validationCode":"import numpy as np\n\ndef as_numeric_multiplier(x):\n    if isinstance(x, (bool, np.bool_)):\n        return int(x)\n    return x","typeGuard":"import numpy as np\n\ndef is_valid_td_multiplier(x) -> bool:\n    return not isinstance(x, (bool, np.bool_))","tryCatchPattern":"try:\n    result = td * other\nexcept TypeError as e:\n    if 'by bool' in str(e):\n        result = td * int(other)\n    else:\n        raise","preventionTips":["Never pass raw bool/np.bool_ as a timedelta multiplier; cast with int().","Audit comparison/mask results before using them in arithmetic."],"tags":["timedelta","multiplication","bool","typeerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}