pandas-dev/pandas · error · OutOfBoundsTimedelta

Overflow in int64 multiplication

Error message

Overflow in int64 multiplication

What it means

Raised by TimedeltaArray._mul_int_overflowsafe, which delegates to the cython mul_overflowsafe for safe int64 multiplication. When that low-level routine detects a product exceeding int64 range it raises OverflowError; pandas re-raises it as OutOfBoundsTimedelta to stay consistent with its other timedelta overflow paths. This covers the array-by-int path (scalar int overflow is caught earlier in __mul__).

Solutions

  1. Scale down the integer multiplier(s) so every element's product stays within ±2**63 ns.
  2. Switch to a float multiplier and operate in coarser units (total_seconds) if you need magnitudes beyond the ns int64 range.
  3. Clip either operand to a safe range before multiplying.

Example fix

# before
pd.to_timedelta(np.arange(5), unit='D') * np.full(5, 10**18)  # OutOfBoundsTimedelta

# after
pd.to_timedelta(np.arange(5), unit='D') * np.array([1, 2, 3, 4, 5])
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def td_int_mul_is_safe(td_ns_values, int_arr):
    a = np.asarray(td_ns_values, dtype='i8')
    b = np.asarray(int_arr, dtype='i8')
    # NaT-safe extreme bound
    a_safe = np.where(a == np.int64(np.iinfo(np.int64).min), 0, a)
    lo = int(a_safe.min()) * int(b.min())
    hi = int(a_safe.max()) * int(b.max())
    return max(abs(lo), abs(hi)) <= np.iinfo(np.int64).max

Type guard

null

Try / catch

from pandas.errors import OutOfBoundsTimedelta
try:
    result = td * int_weights
except OutOfBoundsTimedelta:
    result = td.dt.total_seconds() * int_weights

Prevention

When it happens

Trigger: Multiplying a timedelta64 array by an int array where at least one product exceeds ±2**63 nanoseconds, e.g. `pd.to_timedelta(np.arange(10), unit='D') * np.array([10**18], dtype='i8')`.

Common situations: Vectorized scaling of durations by large integer weights; broadcasting an int array/Series multiplier against ns-resolution timedeltas.

Related errors


AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11). Data as JSON: /api/errors/c43dd40e4e3c9057. Report an issue: GitHub.

Appendix: source

Thrown at pandas/core/arrays/timedeltas.py:511

        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)
        return type(self)._simple_new(result, dtype=result.dtype)

    @unpack_zerodim_and_defer("__mul__")
    def __mul__(self, other) -> Self:
        if is_scalar(other):
            if lib.is_bool(other):
                raise TypeError(
                    f"Cannot multiply '{self.dtype}' by bool, explicitly cast to "
                    "integers instead"
                )
            other = _exact_if_integral(other)
            if lib.is_integer(other):
                # GH#43178: detect int64 overflow rather than silently wrapping
                #  in the i8 cast below (e.g. a multiplier outside int64 bounds).
                # TODO(numpy>=2.5): numpy detects this natively (numpy GH-31378)
                #  but raises OverflowError; once the numpy floor is >= 2.5, drop
                #  mul_overflowsafe and re-wrap numpy's error as

View on GitHub (pinned to 3b7651241d)