{"record":{"id":"9188262671e25463","repo":"pandas-dev/pandas","slug":"overflow-in-timedelta-operation","errorCode":null,"errorMessage":"overflow in timedelta operation","messagePattern":"overflow in timedelta operation","errorType":"exception","errorClass":"OutOfBoundsTimedelta","httpStatus":null,"severity":"error","filePath":"pandas/core/array_algos/datetimelike_accumulations.py","lineNumber":50,"sourceCode":"    mask : np.ndarray[bool]\n        Positions whose result will be NaT regardless, and so are exempt.\n    \"\"\"\n    # Whether each running total is greater than the one before it, taking the\n    #  total before the first entry to be zero.\n    stepped_up = np.empty(result.shape, dtype=bool)\n    np.greater(result[:1], 0, out=stepped_up[:1])\n    np.greater(result[1:], result[:-1], out=stepped_up[1:])\n\n    # Absent a signed wrap, the total goes up exactly when the addend is\n    #  positive; a wrap flips the direction of the step.\n    invalid = (values > 0) != stepped_up\n    # A total of exactly int64.min does not wrap, but is indistinguishable\n    #  from NaT once stored.\n    invalid |= result == iNaT\n    invalid &= ~mask\n\n    if invalid.any():\n        raise OutOfBoundsTimedelta(\"overflow in timedelta operation\")\n\n\ndef _cum_func(\n    func: Callable,\n    values: np.ndarray,\n    *,\n    skipna: bool = True,\n) -> np.ndarray:\n    \"\"\"\n    Accumulations for 1D datetimelike arrays.\n\n    Parameters\n    ----------\n    func : np.cumsum, np.maximum.accumulate, np.minimum.accumulate\n    values : np.ndarray\n        Numpy array with the values (can be of any dtype that support the\n        operation). Values is changed is modified inplace.\n    skipna : bool, default True","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/array_algos/datetimelike_accumulations.py#L32-L68","documentation":"OutOfBoundsTimedelta raised in datetimelike_accumulations when a cumsum on a timedelta array produces a total whose int64 nanosecond representation wraps (signed overflow) or equals NaT (int64.min). Pandas detects the wrap by checking step direction against the addend sign and refuses to return an incorrect result.","triggerScenarios":"pd.Series(pd.to_timedelta([...])).cumsum() where the running sum exceeds the int64 nanosecond range (~292 years); cumsum on long-running timestamp differences.","commonSituations":"Aggregating many large timedelta values (e.g. days-scale durations over millions of rows); processing log/event durations whose cumulative total passes int64.max nanoseconds.","solutions":["Reduce the magnitude: aggregate at a coarser unit by converting to a smaller dtype after summing per-chunk.","Filter or resample the series so the running total stays within int64 nanosecond range.","Compute cumsum on integer seconds/minutes (s.astype('int64')//unit) and convert back to timedelta at the end."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"NS_INT64_MAX = (2**63 - 1)\n\ndef timedelta_cumsum_safe(s):\n    total_ns = s.dt.total_seconds().sum() * 1e9\n    if abs(total_ns) > NS_INT64_MAX:\n        raise OverflowError('cumsum would overflow int64 nanoseconds; resample or chunk first')\n    return s.cumsum()","typeGuard":null,"tryCatchPattern":"from pandas.errors import OutOfBoundsTimedelta\ntry:\n    s.cumsum()\nexcept OutOfBoundsTimedelta:\n    # fall back to per-chunk cumsum on a coarser unit\n    (s.dt.total_seconds().cumsum() * 1e9).astype('timedelta64[ns]')","preventionTips":["Before cumsum on timedelta, check the total magnitude against int64 nanosecond limits.","Aggregate in chunks or coarser units for long-running durations."],"tags":["timedelta","cumsum","overflow","datetime"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}