{"record":{"id":"49ada9b06b661b76","repo":"pandas-dev/pandas","slug":"dtype-data-dtype-cannot-be-converted-to-timedelt","errorCode":null,"errorMessage":"dtype {data.dtype} cannot be converted to timedelta64[ns]","messagePattern":"dtype (.+?) cannot be converted to timedelta64\\[ns\\]","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/timedeltas.py","lineNumber":1328,"sourceCode":"        data = data.astype(np.float64, copy=False)\n        try:\n            data = cast_from_unit_vectorized(data, unit or \"ns\")\n        except OutOfBoundsDatetime as err:\n            raise OutOfBoundsTimedelta(*err.args) from err\n        data[mask] = iNaT\n        data = data.view(\"m8[ns]\")\n        copy = False\n\n    elif lib.is_np_dtype(data.dtype, \"m\"):\n        if not is_supported_dtype(data.dtype):\n            # cast to closest supported unit, i.e. s or ns\n            new_dtype = get_supported_dtype(data.dtype)\n            data = astype_overflowsafe(data, dtype=new_dtype, copy=False)\n            copy = False\n\n    else:\n        # This includes datetime64-dtype, see GH#23539, GH#29794\n        raise TypeError(f\"dtype {data.dtype} cannot be converted to timedelta64[ns]\")\n\n    if not copy:\n        data = np.asarray(data)\n    else:\n        data = np.array(data, copy=copy)\n\n    assert data.dtype.kind == \"m\"\n    assert data.dtype != \"m8\"  # i.e. not unit-less\n\n    return data\n\n\ndef _ints_to_td64ns(data, unit: str = \"ns\") -> tuple[np.ndarray, bool]:\n    \"\"\"\n    Convert an ndarray with integer-dtype to timedelta64[ns] dtype, treating\n    the integers as multiples of the given timedelta unit.\n\n    Parameters","sourceCodeStart":1310,"sourceCodeEnd":1346,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L1310-L1346","documentation":"Raised in the internal conversion path (the `_td64`-style helper in timedeltas.py) when `data` has a dtype that is neither integer-backed, timedelta64, nor a supported timedelta resolution. The branch explicitly notes datetime64 is included (GH#23539, GH#29794): you cannot reinterpret datetime64 data as timedelta64. The error names the offending dtype so the caller can correct the input.","triggerScenarios":"Passing a datetime64 array/Series into a constructor expecting timedelta64; providing float, object, or string dtype data without prior conversion to timedelta.","commonSituations":"Confusing Timestamp and Timedelta columns during data prep; assuming a dtype view will silently reinterpret datetime data.","solutions":["Convert the data with `pd.to_timedelta(...)` before constructing the timedelta array.","If you hold datetime64 data and need durations, subtract a reference Timestamp to obtain a timedelta64 result rather than re-viewing.","Validate the input dtype and branch to the correct constructor."],"exampleFix":"# before\npd.TimedeltaIndex(np.array(['2020-01-01'], dtype='datetime64[ns]'))  # TypeError\n\n# after\nref = pd.Timestamp('2020-01-01')\npd.to_timedelta((pd.to_datetime(['2020-01-01']) - ref).total_seconds(), unit='s')","handlingStrategy":"type-guard","validationCode":"import numpy as np\n\ndef is_timedelta_convertible(data) -> bool:\n    arr = np.asarray(data)\n    k = arr.dtype.kind\n    # integer, supported timedelta64, or convertible via to_timedelta\n    return k in 'iu' or (k == 'm' and arr.dtype != 'datetime64')","typeGuard":"import numpy as np\n\ndef is_not_datetime_or_object(arr) -> bool:\n    return getattr(arr, 'dtype', None) is not None and arr.dtype.kind not in ('M', 'O', 'U', 'S')","tryCatchPattern":"try:\n    tdi = pd.TimedeltaIndex(data)\nexcept TypeError as e:\n    if 'cannot be converted to timedelta64' in str(e):\n        tdi = pd.to_timedelta(pd.to_numeric(pd.Series(data)), unit='ns')\n    else:\n        raise","preventionTips":["Run data through pd.to_timedelta() before constructing TimedeltaIndex/Array.","Never view datetime64 data as timedelta64; subtract a reference Timestamp instead."],"tags":["timedelta","dtype","conversion","typeerror","datetime"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}