{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/array_algos/datetimelike_accumulations.py#L32-L68","documentation":"Raised by _check_cumsum_overflow (datetimelike_accumulations.py:50) as OutOfBoundsTimedelta when a cumsum on a timedelta64 or datetime64 array would overflow int64. Internally these arrays are stored as int64 nanosecond ticks; cumsum is computed in i8 space. _check_cumsum_overflow detects when a positive addend produced a non-increasing total (a signed wrap) or landed exactly on the NaT sentinel iNaT, which would corrupt the result, so pandas refuses to return a misleading value.","triggerScenarios":"Calling Series.cumsum() (or df.cumsum()) on a timedelta64 Series whose running total exceeds the int64 nanosecond range (~292 years), or on a datetime64 cumulative sum. Triggered at datetimelike_accumulations.py:43-50 when (values > 0) != stepped_up or result == iNaT after np.cumsum in i8 space.","commonSituations":"Aggregating long sequences of large timedeltas (e.g. hours/days cumulated over millions of rows); summing durations that span more than ~292 years; converting large integer counts of days to timedelta then cumsumming; loading wide time-series data.","solutions":["Reduce the magnitude before cumsum: convert timedelta to a coarser unit (e.g. .dt.total_seconds() or astimeunit) and track overflow at that granularity.","Skip the offending rows or downsample/aggregate so the running total stays within int64 ns range.","Compute cumsum on a plain int64 Series of your chosen unit and cast back to timedelta64 only at the end (or never, if you can keep it as int)."],"exampleFix":"// before\ns = pd.Series(pd.to_timedelta(np.full(10_000, 10**17), unit='ns'))\ns.cumsum()  # overflow\n// after\nsecs = s.dt.total_seconds()\n(secs.cumsum() * 1e9).astype('timedelta64[ns]')","handlingStrategy":"try-catch","validationCode":"import numpy as np\nns = s.astype('i8') if hasattr(s, 'dtype') and s.dtype.kind in 'mM' else s\nrunning = np.cumsum(np.asarray(ns))\nstepped_up = np.empty(running.shape, dtype=bool)\nnp.greater(running[:1], 0, out=stepped_up[:1])\nnp.greater(running[1:], running[:-1], out=stepped_up[1:])\nif ((ns > 0) != stepped_up).any():\n    raise OverflowError('cumsum would overflow int64 ns; reduce magnitude or change unit')","typeGuard":null,"tryCatchPattern":"from pandas._libs.tslibs import OutOfBoundsTimedelta\ntry:\n    s.cumsum()\nexcept OutOfBoundsTimedelta:\n    # fall back to a coarser unit\n    (s.dt.total_seconds().cumsum() * 1e9).astype('timedelta64[ns]')","preventionTips":["For long-running timedelta sums, work in seconds/minutes instead of nanoseconds.","Downsample or chunk cumsum operations on large durations."],"tags":["pandas","timedelta","datetime","cumsum","overflow","int64"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}