{"record":{"id":"da96f39dd4770611","repo":"pandas-dev/pandas","slug":"cannot-convert-input-with-unit-unit","errorCode":null,"errorMessage":"Cannot convert input with unit '{unit}'","messagePattern":"Cannot convert input with unit '(.+?)'","errorType":"exception","errorClass":"OutOfBoundsTimedelta","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":1344,"sourceCode":"    Parameters\n    ----------\n    data : numpy.ndarray with integer-dtype\n    unit : str, default \"ns\"\n        The timedelta unit to treat integers as multiples of.\n\n    Returns\n    -------\n    numpy.ndarray : timedelta64[ns] array converted from data\n    bool : whether a copy was made\n    \"\"\"\n    copy_made = False\n    unit = unit if unit is not None else \"ns\"\n\n    if data.dtype != np.int64:\n        # GH#60677 unsigned integers > int64 max overflow silently\n        # when cast to int64 (which timedelta64 is backed by)\n        if data.dtype == np.dtype(\"uint64\") and (data > np.iinfo(np.int64).max).any():\n            raise OutOfBoundsTimedelta(f\"Cannot convert input with unit '{unit}'\")\n        # converting to int64 makes a copy, so we can avoid\n        # re-copying later\n        data = data.astype(np.int64)\n        copy_made = True\n\n    if unit != \"ns\":\n        dtype_str = f\"timedelta64[{unit}]\"\n        data = data.view(dtype_str)\n\n        new_dtype = get_supported_dtype(data.dtype)\n        if new_dtype != data.dtype:\n            data = astype_overflowsafe(data, dtype=new_dtype)\n\n            # the astype conversion makes a copy, so we can avoid re-copying later\n            copy_made = True\n\n    else:\n        data = data.view(\"timedelta64[ns]\")","sourceCodeStart":1326,"sourceCodeEnd":1362,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L1326-L1362","documentation":"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.","triggerScenarios":"`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.","commonSituations":"uint64 epoch-nanoseconds or epoch-seconds sourced from databases/parquet; overflow when treating huge unsigned counts as durations.","solutions":["Cast to float64 before conversion: `arr.astype('float64')`.","Clip to int64 max: `np.minimum(arr, np.iinfo(np.int64).max).astype('int64')`.","Reconsider unit choice if values are timestamps rather than durations."],"exampleFix":"// before\ns = pd.to_timedelta(uint_arr, unit='ns')  # > int64.max\n\n// after\ns = pd.to_timedelta(uint_arr.astype('float64'), unit='ns')","handlingStrategy":"validation","validationCode":"import numpy as np\narr = np.asarray(data)\nif arr.dtype == np.uint64 and (arr > np.iinfo(np.int64).max).any():\n    data = arr.astype('float64')","typeGuard":"def fits_int64(arr) -> bool:\n    import numpy as np\n    a = np.asarray(arr)\n    return a.dtype != np.uint64 or bool((a <= np.iinfo(np.int64).max).all())","tryCatchPattern":"try:\n    s = pd.to_timedelta(arr, unit=unit)\nexcept OutOfBoundsTimedelta as e:\n    if 'Cannot convert input with unit' in str(e):\n        s = pd.to_timedelta(arr.astype('float64'), unit=unit)\n    else:\n        raise","preventionTips":["Downcast uint64 columns before timedeltas.","Clip large unsigned values.","Prefer int64 at ingestion for epoch-like data."],"tags":["timedelta","overflow","uint64","conversion","gh-60677"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}