pandas-dev/pandas · error · OutOfBoundsTimedelta

Cannot convert input with unit '{unit}'

Error message

Cannot convert input with unit '{unit}'

What it means

Raised as OutOfBoundsTimedelta by _ints_to_td64ns when an unsigned-int64 array contains values exceeding int64.max (GH#60677). Since timedelta64 is int64-backed, those values cannot be represented; pandas refuses rather than silently wrapping to negative.

Source

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

    Parameters
    ----------
    data : numpy.ndarray with integer-dtype
    unit : str, default "ns"
        The timedelta unit to treat integers as multiples of.

    Returns
    -------
    numpy.ndarray : timedelta64[ns] array converted from data
    bool : whether a copy was made
    """
    copy_made = False
    unit = unit if unit is not None else "ns"

    if data.dtype != np.int64:
        # GH#60677 unsigned integers > int64 max overflow silently
        # when cast to int64 (which timedelta64 is backed by)
        if data.dtype == np.dtype("uint64") and (data > np.iinfo(np.int64).max).any():
            raise OutOfBoundsTimedelta(f"Cannot convert input with unit '{unit}'")
        # converting to int64 makes a copy, so we can avoid
        # re-copying later
        data = data.astype(np.int64)
        copy_made = True

    if unit != "ns":
        dtype_str = f"timedelta64[{unit}]"
        data = data.view(dtype_str)

        new_dtype = get_supported_dtype(data.dtype)
        if new_dtype != data.dtype:
            data = astype_overflowsafe(data, dtype=new_dtype)

            # the astype conversion makes a copy, so we can avoid re-copying later
            copy_made = True

    else:
        data = data.view("timedelta64[ns]")

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Cast to float64 before conversion: `arr.astype('float64')`.
  2. Clip to int64 max: `np.minimum(arr, np.iinfo(np.int64).max).astype('int64')`.
  3. Reconsider unit choice if values are timestamps rather than durations.

Example fix

// before
s = pd.to_timedelta(uint_arr, unit='ns')  # > int64.max

// after
s = pd.to_timedelta(uint_arr.astype('float64'), unit='ns')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np
arr = np.asarray(data)
if arr.dtype == np.uint64 and (arr > np.iinfo(np.int64).max).any():
    data = arr.astype('float64')

Type guard

def fits_int64(arr) -> bool:
    import numpy as np
    a = np.asarray(arr)
    return a.dtype != np.uint64 or bool((a <= np.iinfo(np.int64).max).all())

Try / catch

try:
    s = pd.to_timedelta(arr, unit=unit)
except OutOfBoundsTimedelta as e:
    if 'Cannot convert input with unit' in str(e):
        s = pd.to_timedelta(arr.astype('float64'), unit=unit)
    else:
        raise

Prevention

When it happens

Trigger: `pd.to_timedelta(np.array([2**63], dtype='uint64'), unit='s')`, or constructing a TimedeltaIndex/Timedelta from uint64 values beyond 2**63-1 with a unit.

Common situations: uint64 epoch-nanoseconds or epoch-seconds sourced from databases/parquet; overflow when treating huge unsigned counts as durations.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/da96f39dd4770611. Report an issue: GitHub.