pandas-dev/pandas · error · OutOfBoundsTimedelta
Overflow in timedelta multiplication
Error message
Overflow in timedelta multiplication
What it means
Raised by TimedeltaArray._mul_float_overflowsafe when multiplying a timedelta64 array by a float whose products would exceed the int64 nanosecond range (~±292 years). Because numpy's int64-backed timedelta silently saturates at int64.max, pandas pre-checks the float64 product against 2**63 (not i8max, since i8max rounds up to 2**63 in float64) and raises OutOfBoundsTimedelta instead. NaT positions and NaN products are masked out before the bound check.
Solutions
- Reduce the multiplier so all products stay within ±2**63 nanoseconds (~292.3 years from zero).
- Convert the timedelta to a coarser representation (e.g. compute in seconds via `.dt.total_seconds()`) before scaling, then cast back if needed.
- Cap or clip extreme source values with `.clip()` before multiplication.
Example fix
# before pd.to_timedelta(10**17, unit='ns') * 1e6 # OutOfBoundsTimedelta # after (pd.to_timedelta(10**17, unit='ns') / 1e6).total_seconds()
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
NS_INT64_MAX = 2**63
def td_float_mul_is_safe(td_ns_values, factor):
arr = np.asarray(td_ns_values, dtype='i8')
arr = np.where(arr == np.int64(np.iinfo(np.int64).min), 0, arr)
return np.max(np.abs(arr.astype('f8') * factor), initial=0.0) < NS_INT64_MAX Type guard
null
Try / catch
from pandas.errors import OutOfBoundsTimedelta
try:
result = td * factor
except OutOfBoundsTimedelta:
# operate in coarser units to avoid the ns int64 ceiling
result = (td.dt.total_seconds() * factor) Prevention
- Prefer total_seconds()-based scaling for large magnitudes.
- Bound-check float factors against the ±2**63 ns product before multiplying.
When it happens
Trigger: Multiplying a large-magnitude timedelta by a large float: `pd.to_timedelta(10**17, unit='ns') * 1e6`, or scaling a long-duration Series by a float that pushes any element past ±2**63 nanoseconds.
Common situations: Unit-conversion arithmetic that accidentally uses ns-resolution magnitudes; aggregating or rescaling long durations (days/weeks) by large float weights.
Related errors
- Overflow in int64 multiplication
- Cannot convert input with unit
- Overflow in timedelta division
- Cannot multiply ' ' by bool, explicitly cast to integers…
- Cannot multiply with
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/1ab8e29e7f3fd08b.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/timedeltas.py:494
def _mul_float_overflowsafe(
self, other: float | np.floating | npt.NDArray[np.floating]
) -> Self:
# GH#43178: detect float products that would silently saturate to
# int64.max on the int64 cast below
i8 = self.asi8
self_mask = i8 == iNaT
if self_mask.any():
# zero out NaT positions so they don't trigger the bounds check
i8 = np.where(self_mask, 0, i8)
f_result = i8 * other
nan_mask = np.isnan(f_result)
non_nan = f_result[~nan_mask]
# Compare against 2**63, not i8max: i8max (2**63 - 1) rounds up to
# 2**63 in float64, so a product landing exactly on 2**63 would slip
# past a ``> i8max`` check and saturate on the cast. Also catches +/-inf.
if non_nan.size and np.max(np.abs(non_nan), initial=0.0) >= 2.0**63:
raise OutOfBoundsTimedelta("Overflow in timedelta multiplication")
# NaN-to-int cast is platform-dependent; substitute 0 then re-mask as NaT
if nan_mask.any():
f_result = np.where(nan_mask, 0.0, f_result)
i8_result = f_result.astype("i8")
nat_out = self_mask | nan_mask
if nat_out.any():
i8_result[nat_out] = iNaT
result = i8_result.view(self._ndarray.dtype)
return type(self)._simple_new(result, dtype=result.dtype)
def _mul_int_overflowsafe(self, i8_other: npt.NDArray[np.int64]) -> Self:
# GH#43178: mul_overflowsafe raises the low-level OverflowError; surface
# it as OutOfBoundsTimedelta to match pandas' other td64 overflow paths.
try:
i8_result = mul_overflowsafe(self.asi8, i8_other)
except OverflowError as err:
raise OutOfBoundsTimedelta("Overflow in int64 multiplication") from err
result = i8_result.view(self._ndarray.dtype)View on GitHub (pinned to 3b7651241d)