pandas-dev/pandas · error · OutOfBoundsTimedelta

Cannot convert input with unit

Error message

Cannot convert input with unit '{unit}'

What it means

Raised in the integer-with-unit conversion helper when the input is uint64 and at least one value exceeds int64.max, because casting such values to int64 (the backing type of timedelta64) silently overflows. pandas detects this before the cast and raises OutOfBoundsTimedelta naming the requested unit, rather than producing a wrapped/invalid duration.

Solutions

  1. Convert the source to float64 or int64 before construction, after confirming the magnitudes represent valid durations.
  2. Filter or clip values exceeding 2**63 - 1.
  3. Choose a coarser unit so the per-element nanosecond count stays within int64 range.

Example fix

# before
pd.to_timedelta(np.array([2**63 + 1], dtype='u8'), unit='s')  # OutOfBoundsTimedelta

# after
pd.to_timedelta(np.array([2**63 + 1], dtype='f8'), unit='s')
Defensive patterns

Strategy: validation

Validate before calling

import numpy as np

def uint64_fits_i64_for_unit(arr):
    arr = np.asarray(arr)
    if arr.dtype != np.dtype('u8'):
        return True
    return not (arr > np.iinfo(np.int64).max).any()

Type guard

null

Try / catch

from pandas.errors import OutOfBoundsTimedelta
try:
    result = pd.to_timedelta(values, unit=unit)
except OutOfBoundsTimedelta as e:
    if 'Cannot convert input with unit' in str(e):
        result = pd.to_timedelta(values.astype('f8'), unit=unit)
    else:
        raise

Prevention

When it happens

Trigger: `pd.to_timedelta(np.array([2**63 + 1], dtype='u8'), unit='s')`, or constructing a timedelta from a uint64 series whose magnitude exceeds 2**63 - 1 in the given unit.

Common situations: Ingested counters or large unsigned magnitudes passed straight into timedelta construction; treating uint64 timestamps as durations.

Related errors


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

Appendix: source

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

    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 3b7651241d)