{"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":644,"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            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\n","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L626-L662","documentation":"Raised by TimedeltaArray._scalar_divlike_op for reverse true/floor division: dividing a non-timedelta scalar (numeric) by a timedelta is mathematically undefined in pandas' model, so a TypeError names both types. e.g. `5 / pd.Timedelta('1d')` is meaningless without a target unit.","triggerScenarios":"Calling `int / timedelta` or `float / timedelta`, or `int // timedelta_array`. Triggers in the reverse ops rtruediv/rfloordiv branch when other is a scalar not in _recognized_scalars.","commonSituations":"Confusing the operand order; trying to express 'how many periods fit in N' by dividing a count by a duration instead of the reverse.","solutions":["Swap operand order: divide the timedelta by the scalar to get a scaled timedelta.","If you want a count, divide the timedelta by another timedelta: `td_a / td_b`.","For numeric output use .dt.total_seconds() then do ordinary division."],"exampleFix":"// before\ncount = 5 / pd.Timedelta('2h')\n\n// after\ncount = pd.Timedelta('10h') / pd.Timedelta('2h')  # -> 5.0","handlingStrategy":"type-guard","validationCode":"import numbers\nif isinstance(other, numbers.Number) and not hasattr(other, 'dtype'):\n    # ensure divisor is the timedelta, not the dividend\n    raise TypeError('reverse numeric/timedelta division is unsupported; swap operands')","typeGuard":"def is_valid_td_dividend(x) -> bool:\n    import pandas as pd\n    return isinstance(x, (pd.Timedelta, pd.TimedeltaIndex)) or (\n        hasattr(x, 'dtype') and x.dtype.kind == 'm')","tryCatchPattern":"try:\n    out = scalar / td\nexcept TypeError as e:\n    if 'Cannot divide' in str(e) and 'by' in str(e):\n        out = td / scalar  # swap to forward op\n    else:\n        raise","preventionTips":["Keep the timedelta on the left of /.","For counts use timedelta / timedelta.","Use .dt.total_seconds() for numeric reductions."],"tags":["timedelta","typeerror","division","operand-order"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}