{"record":{"id":"84308a04c927d1b7","repo":"pandas-dev/pandas","slug":"cannot-divide-type-other-name-by-type-self","errorCode":null,"errorMessage":"Cannot divide {type(other).__name__} by {type(self).__name__}","messagePattern":"Cannot divide (.+?) by (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":663,"sourceCode":"        if isinstance(other, self._recognized_scalars):\n            other = Timedelta(other)\n            # mypy assumes that __new__ returns an instance of the class\n            # github.com/python/mypy/issues/1020\n            if cast(\"Timedelta | NaTType\", other) is NaT:\n                # specifically timedelta64-NaT\n                res = np.empty(self.shape, dtype=np.float64)\n                res.fill(np.nan)\n                return res\n\n            # otherwise, dispatch to Timedelta implementation\n            return op(self._ndarray, other)\n\n        else:\n            # caller is responsible for checking lib.is_scalar(other)\n            # assume other is numeric, otherwise numpy will raise\n\n            if op in [roperator.rtruediv, roperator.rfloordiv]:\n                raise TypeError(\n                    f\"Cannot divide {type(other).__name__} by {type(self).__name__}\"\n                )\n\n            other = _exact_if_integral(other)\n            if lib.is_float(other):\n                # GH#43178: raise instead of silently saturating on overflow\n                self._check_float_div_overflow(other)\n            result = op(self._ndarray, other)\n            return type(self)._simple_new(result, dtype=result.dtype)\n\n    def _cast_divlike_op(self, other):\n        if not hasattr(other, \"dtype\"):\n            # e.g. list, tuple\n            other = np.array(other)\n\n        if len(other) != len(self):\n            raise ValueError(\"Cannot divide vectors with unequal lengths\")\n        return other","sourceCodeStart":645,"sourceCodeEnd":681,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L645-L681","documentation":"Raised in TimedeltaArray._scalar_divlike_op when the operation is a reversed true/floor division (rtruediv/rfloordiv) and the scalar operand is numeric rather than a timedelta. Dividing a plain number by a duration has no well-defined result in pandas' timedelta semantics, so it is rejected as TypeError. Timezone/timedelta-typed operands take the earlier Timedelta-dispatch branch instead.","triggerScenarios":"`5 / pd.to_timedelta([1, 2], unit='D')`, or `10 // pd.Timedelta('1D')`-style reverse division of a scalar number by a timedelta array.","commonSituations":"Writing division expressions in the wrong operand order; expecting rate (1/time) semantics that pandas does not provide for scalar numerators.","solutions":["Reverse the expression: divide the timedelta by the scalar instead, if that matches intent.","If a 1/duration rate is genuinely needed, compute it in numeric space (e.g. `1.0 / td.dt.total_seconds()`).","Reorder operands so the timedelta is the numerator."],"exampleFix":"# before\n5 / pd.to_timedelta([1, 2], unit='D')  # TypeError\n\n# after (compute rate numerically)\n1.0 / pd.to_timedelta([1, 2], unit='D').total_seconds()","handlingStrategy":"type-guard","validationCode":"import numbers, numpy as np\nfrom pandas import Timedelta\n\ndef is_reverse_div_valid(numerator, td_array) -> bool:\n    # reverse div is only valid when numerator is itself timedelta-like\n    return isinstance(numerator, (Timedelta, np.timedelta64))","typeGuard":"import numpy as np\nfrom pandas import Timedelta\n\ndef can_rdiv_td(numerator) -> bool:\n    return isinstance(numerator, (Timedelta, np.timedelta64))","tryCatchPattern":"try:\n    result = number / td_array\nexcept TypeError as e:\n    if 'Cannot divide' in str(e):\n        result = 1.0 / td_array.dt.total_seconds() * number\n    else:\n        raise","preventionTips":["Order division so the timedelta is the numerator.","Compute rate-style results in numeric (total_seconds) space."],"tags":["timedelta","division","typeerror","reverse-division"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}