{"record":{"id":"6eb971b09ba90634","repo":"pandas-dev/pandas","slug":"cannot-generate-range-with-side-endpoint-and-p","errorCode":null,"errorMessage":"Cannot generate range with {side}={endpoint} and periods={periods}","messagePattern":"Cannot generate range with (.+?)=(.+?) and periods=(.+?)","errorType":"exception","errorClass":"OutOfBoundsDatetime","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/_ranges.py","lineNumber":227,"sourceCode":"    other_end : int\n\n    Raises\n    ------\n    OutOfBoundsDatetime\n    \"\"\"\n    # GH#14187 raise instead of incorrectly wrapping around\n    assert side in [\"start\", \"end\"]\n\n    i64max = np.uint64(i8max)\n    msg = f\"Cannot generate range with {side}={endpoint} and periods={periods}\"\n\n    with np.errstate(over=\"raise\"):\n        # if periods * strides cannot be multiplied within the *uint64* bounds,\n        #  we cannot salvage the operation by recursing, so raise\n        try:\n            addend = np.uint64(periods) * np.uint64(np.abs(stride))\n        except FloatingPointError as err:\n            raise OutOfBoundsDatetime(msg) from err\n\n    if np.abs(addend) <= i64max:\n        # relatively easy case without casting concerns\n        return _generate_range_overflow_safe_signed(endpoint, periods, stride, side)\n\n    elif (endpoint > 0 and side == \"start\" and stride > 0) or (\n        endpoint < 0 < stride and side == \"end\"\n    ):\n        # no chance of not-overflowing\n        raise OutOfBoundsDatetime(msg)\n\n    elif side == \"end\" and endpoint - stride <= i64max < endpoint:\n        # in _generate_regular_range we added `stride` thereby overflowing\n        #  the bounds.  Adjust to fix this.\n        return _generate_range_overflow_safe(\n            endpoint - stride, periods - 1, stride, side\n        )\n","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/_ranges.py#L209-L245","documentation":"`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.","triggerScenarios":"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'`.","commonSituations":"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.","solutions":["Reduce `periods`, or shrink the stride (lower `freq`).","Switch to a coarser `unit` ('s', 'ms', 'us') so the i8 horizon is far larger.","Bound `periods` upstream and validate against `(2**63 - 1) // stride` before calling `date_range`.","If you only need `start` and `end`, omit `periods` and pass `freq` so the count is derived."],"exampleFix":"// before\npd.date_range('2000-01-01', periods=10**18, freq='D')\n# periods*stride overflows -> OutOfBoundsDatetime\n\n// after\npd.date_range('2000-01-01', periods=10**6, freq='D', unit='s')","handlingStrategy":"validation","validationCode":"from pandas._libs.lib import i8max\nfrom pandas import Timedelta\nstride = abs(Timedelta(freq).as_unit('ns')._value)\nif stride > 0 and periods > i8max // stride:\n    raise ValueError(f'periods too large for freq {freq}; max ~ {i8max // stride}')\npd.date_range(start, periods=periods, freq=freq)","typeGuard":"def periods_within_bounds(periods, freq, unit='ns') -> bool:\n    from pandas._libs.lib import i8max\n    from pandas import Timedelta\n    stride = abs(Timedelta(freq).as_unit(unit, round_ok=False)._value)\n    return stride == 0 or periods <= i8max // stride","tryCatchPattern":"from pandas.errors import OutOfBoundsDatetime\ntry:\n    rng = pd.date_range(start, periods=periods, freq=freq)\nexcept OutOfBoundsDatetime as e:\n    if 'Cannot generate range' in str(e):\n        rng = pd.date_range(start, periods=min(periods, 10**6), freq=freq, unit='s')\n    else:\n        raise","preventionTips":["Bound periods from untrusted input","Use a coarser unit to extend the representable window","Prefer end=+freq over start+periods for very long ranges"],"tags":["date-range","overflow","out-of-bounds","periods"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}