{"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":1364,"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":1346,"sourceCodeEnd":1382,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L1346-L1382","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Ingested counters or large unsigned magnitudes passed straight into timedelta construction; treating uint64 timestamps as durations.","solutions":["Convert the source to float64 or int64 before construction, after confirming the magnitudes represent valid durations.","Filter or clip values exceeding 2**63 - 1.","Choose a coarser unit so the per-element nanosecond count stays within int64 range."],"exampleFix":"# before\npd.to_timedelta(np.array([2**63 + 1], dtype='u8'), unit='s')  # OutOfBoundsTimedelta\n\n# after\npd.to_timedelta(np.array([2**63 + 1], dtype='f8'), unit='s')","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef uint64_fits_i64_for_unit(arr):\n    arr = np.asarray(arr)\n    if arr.dtype != np.dtype('u8'):\n        return True\n    return not (arr > np.iinfo(np.int64).max).any()","typeGuard":"null","tryCatchPattern":"from pandas.errors import OutOfBoundsTimedelta\ntry:\n    result = pd.to_timedelta(values, unit=unit)\nexcept OutOfBoundsTimedelta as e:\n    if 'Cannot convert input with unit' in str(e):\n        result = pd.to_timedelta(values.astype('f8'), unit=unit)\n    else:\n        raise","preventionTips":["Cast uint64 inputs to int64/float64 after confirming magnitudes before timedelta construction.","Choose a coarser unit to keep per-element ns counts within int64 range."],"tags":["timedelta","overflow","unsigned","uint64","outofbounds","units"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}