{"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":638,"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":620,"sourceCodeEnd":656,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L620-L656","documentation":"Raised by TimedeltaArray._check_float_div_overflow when dividing a timedelta64 array by a float whose quotient would exceed int64 nanosecond range. numpy's timedelta/float division silently saturates past int64 bounds, so pandas computes the float64 quotient (ignoring NaT/zero/NaN) and raises OutOfBoundsTimedelta if any active quotient reaches 2**63. The routine first tries an extreme-element fast path before falling back to the per-element check.","triggerScenarios":"`pd.to_timedelta(10**17, unit='ns') / 1e-6`, or dividing a large-duration Series by a tiny float divisor.","commonSituations":"Normalizing durations by very small float factors; unit conversions that expand ns magnitudes; aggregations producing tiny divisors.","solutions":["Increase the float divisor so quotients stay within ±2**63 ns.","Convert to coarser units (e.g. total_seconds) before the division so the effective quotient fits int64.","Clip the source timedelta values before dividing."],"exampleFix":"# before\npd.to_timedelta(10**17, unit='ns') / 1e-6  # OutOfBoundsTimedelta\n\n# after\n(pd.to_timedelta(10**17, unit='ns').total_seconds()) / 1e-6","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef td_float_div_is_safe(td_ns_values, divisor):\n    a = np.asarray(td_ns_values, dtype='i8')\n    d = np.asarray(divisor, dtype='f8')\n    a_safe = np.where(a == np.int64(np.iinfo(np.int64).min), 0, a)\n    with np.errstate(divide='ignore', invalid='ignore'):\n        q = a_safe / d\n    mask = np.isnan(q) | (d == 0)\n    return np.max(np.abs(q), initial=0.0, where=~mask) < 2**63","typeGuard":"null","tryCatchPattern":"from pandas.errors import OutOfBoundsTimedelta\ntry:\n    result = td / divisor\nexcept OutOfBoundsTimedelta:\n    result = td.dt.total_seconds() / divisor","preventionTips":["Bound-check float divisors against the ±2**63 ns quotient.","Use coarser-unit (total_seconds) division for tiny divisors."],"tags":["timedelta","overflow","division","outofbounds"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}