{"record":{"id":"f1c515789a6be0c1","repo":"pandas-dev/pandas","slug":"overflow-in-timedelta-division","errorCode":null,"errorMessage":"Overflow in timedelta division","messagePattern":"Overflow in timedelta division","errorType":"exception","errorClass":"OutOfBoundsTimedelta","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":619,"sourceCode":"        other_arr = np.asarray(other)\n        if other_arr.ndim == 0 and i8.size:\n            divisor = other_arr.item()\n            if divisor == 0 or np.isnan(divisor):\n                # numpy returns all-NaT; nothing to check\n                return\n            # The extreme elements bound all quotients, so most cases resolve\n            #  without the full per-element check below. A NaT (int64.min)\n            #  dividend can only false-trip this bound, never pass an\n            #  overflowing quotient.\n            low_quot = i8.min() / divisor\n            high_quot = i8.max() / divisor\n            if max(abs(low_quot), abs(high_quot)) < 2.0**63:\n                return\n        with np.errstate(divide=\"ignore\", invalid=\"ignore\"):\n            f_quot = i8 / other_arr\n        exclude_mask = (i8 == iNaT) | np.isnan(f_quot) | (other_arr == 0)\n        if np.max(np.abs(f_quot), initial=0.0, where=~exclude_mask) >= 2.0**63:\n            raise OutOfBoundsTimedelta(\"Overflow in timedelta division\")\n\n    def _scalar_divlike_op(self, other, op):\n        \"\"\"\n        Shared logic for __truediv__, __rtruediv__, __floordiv__, __rfloordiv__\n        with scalar 'other'.\n        \"\"\"\n        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)","sourceCodeStart":601,"sourceCodeEnd":637,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L601-L637","documentation":"Raised as OutOfBoundsTimedelta from _check_float_div_overflow when dividing a timedelta array by a float whose quotient (in nanoseconds) would exceed int64 bounds. Numpy would otherwise silently saturate; pandas raises instead (GH#43178). NaT dividends and zero/NaN divisors are excluded since numpy returns NaT for them.","triggerScenarios":"Dividing very large timedeltas by very small floats, e.g. `pd.to_timedelta(['100000d']) / 1e-9`, or `td_array / tiny_float_array`. Quotient in nanoseconds must exceed 2**63.","commonSituations":"Unit conversions that magnify values (days-to-nanoseconds via small divisors); accidental division by sub-second floats; mixing units in pipelines.","solutions":["Convert via to_timedelta with explicit unit instead of arithmetic division.","Increase the divisor (use coarser units) or reduce the dividend magnitude.","If overflow is expected, switch to float64 representation of seconds before dividing."],"exampleFix":"// before\nout = td_series / 1e-9  # magnifies to ns, overflows\n\n// after\nout = td_series.dt.total_seconds() / 1e-9","handlingStrategy":"validation","validationCode":"import numpy as np\ni8 = td.asi8 if hasattr(td, 'asi8') else td.values.view('i8')\nq = np.asarray(i8) / float(divisor)\nif not np.all(np.abs(q[~np.isnan(q)]) < 2.0**63):\n    raise ValueError('division would overflow int64 timedelta bounds')","typeGuard":"def would_overflow_td_div(td_arr, divisor) -> bool:\n    import numpy as np\n    i8 = np.asarray(td_arr).view('i8') if np.asarray(td_arr).dtype.kind == 'm' else None\n    if i8 is None: return False\n    q = i8 / float(divisor)\n    return bool(np.nanmax(np.abs(q)) >= 2.0**63) if q.size else False","tryCatchPattern":"try:\n    out = td / divisor\nexcept OutOfBoundsTimedelta as e:\n    if 'Overflow in timedelta division' in str(e):\n        out = td.dt.total_seconds() / divisor\n    else:\n        raise","preventionTips":["Prefer unit-aware conversions via to_timedelta over raw division.","Bound divisor magnitudes in unit-conversion helpers.","Switch to float64 seconds when ns would overflow."],"tags":["timedelta","overflow","division","float","gh-43178"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}