pandas-dev/pandas · error · OutOfBoundsDatetime

Cannot generate range with {side}={endpoint} and periods={pe

Error message

Cannot generate range with {side}={endpoint} and periods={periods}

What it means

`OutOfBoundsDatetime('Cannot generate range with {side}={endpoint} and periods={periods}')` from `_generate_range_overflow_safe`. The first overflow gate: `np.uint64(periods) * np.uint64(abs(stride))` is computed under `np.errstate(over='raise')`; if the product overflows uint64, even splitting cannot help, so pandas raises immediately. Also raised by the explicit 'no chance of not-overflowing' branch when signs make overflow inevitable.

Source

Thrown at pandas/core/arrays/_ranges.py:227

    other_end : int

    Raises
    ------
    OutOfBoundsDatetime
    """
    # GH#14187 raise instead of incorrectly wrapping around
    assert side in ["start", "end"]

    i64max = np.uint64(i8max)
    msg = f"Cannot generate range with {side}={endpoint} and periods={periods}"

    with np.errstate(over="raise"):
        # if periods * strides cannot be multiplied within the *uint64* bounds,
        #  we cannot salvage the operation by recursing, so raise
        try:
            addend = np.uint64(periods) * np.uint64(np.abs(stride))
        except FloatingPointError as err:
            raise OutOfBoundsDatetime(msg) from err

    if np.abs(addend) <= i64max:
        # relatively easy case without casting concerns
        return _generate_range_overflow_safe_signed(endpoint, periods, stride, side)

    elif (endpoint > 0 and side == "start" and stride > 0) or (
        endpoint < 0 < stride and side == "end"
    ):
        # no chance of not-overflowing
        raise OutOfBoundsDatetime(msg)

    elif side == "end" and endpoint - stride <= i64max < endpoint:
        # in _generate_regular_range we added `stride` thereby overflowing
        #  the bounds.  Adjust to fix this.
        return _generate_range_overflow_safe(
            endpoint - stride, periods - 1, stride, side
        )

View on GitHub (pinned to 3b7651241d)

Solutions

  1. Reduce `periods`, or shrink the stride (lower `freq`).
  2. Switch to a coarser `unit` ('s', 'ms', 'us') so the i8 horizon is far larger.
  3. Bound `periods` upstream and validate against `(2**63 - 1) // stride` before calling `date_range`.
  4. If you only need `start` and `end`, omit `periods` and pass `freq` so the count is derived.

Example fix

// before
pd.date_range('2000-01-01', periods=10**18, freq='D')
# periods*stride overflows -> OutOfBoundsDatetime

// after
pd.date_range('2000-01-01', periods=10**6, freq='D', unit='s')
Defensive patterns

Strategy: validation

Validate before calling

from pandas._libs.lib import i8max
from pandas import Timedelta
stride = abs(Timedelta(freq).as_unit('ns')._value)
if stride > 0 and periods > i8max // stride:
    raise ValueError(f'periods too large for freq {freq}; max ~ {i8max // stride}')
pd.date_range(start, periods=periods, freq=freq)

Type guard

def periods_within_bounds(periods, freq, unit='ns') -> bool:
    from pandas._libs.lib import i8max
    from pandas import Timedelta
    stride = abs(Timedelta(freq).as_unit(unit, round_ok=False)._value)
    return stride == 0 or periods <= i8max // stride

Try / catch

from pandas.errors import OutOfBoundsDatetime
try:
    rng = pd.date_range(start, periods=periods, freq=freq)
except OutOfBoundsDatetime as e:
    if 'Cannot generate range' in str(e):
        rng = pd.date_range(start, periods=min(periods, 10**6), freq=freq, unit='s')
    else:
        raise

Prevention

When it happens

Trigger: Calling `pd.date_range`/`pd.period_range` with `periods` and a `freq` stride so large that `periods * abs(stride)` exceeds 2**64 nanoseconds; e.g. huge `periods` combined with a multi-year offset at `unit='ns'`. Reached via the `_generate_range_overflow_safe` helper for both `side='start'` and `side='end'`.

Common situations: User-supplied `periods` from untrusted input with no upper bound; accidentally passing days/seconds as `periods` that should have been the stride; unit='ns' (the only unit that can hit i8 bounds) with a long horizon.

Related errors


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