{"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":1308,"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":1290,"sourceCodeEnd":1326,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L1290-L1326","documentation":"Raised by sequence_to_td64ns when data.dtype is not integer, float, object, or timedelta64. The else-branch explicitly lists datetime64 as a known trigger (GH#23539, GH#29794): you cannot convert a datetime array into a timedelta array directly.","triggerScenarios":"`pd.to_timedelta(pd.to_datetime(['2020-01-01']))`, or passing a datetime64 ndarray / complex-dtype array to to_timedelta / TimedeltaIndex constructor.","commonSituations":"Confusing duration vs timestamp; subtracting two datetimes but forgetting to wrap with subtraction that yields timedelta; passing the wrong column from a pipeline.","solutions":["If you have datetimes, compute differences: `dt_a - dt_b` already yields timedelta.","Cast object columns of strings first via pd.to_timedelta on the raw strings.","Check data.dtype before calling to_timedelta and branch accordingly."],"exampleFix":"// before\ns = pd.to_timedelta(df['timestamp'])  # datetime64\n\n// after\ns = df['timestamp'] - df['timestamp'].min()  # timedelta","handlingStrategy":"type-guard","validationCode":"import numpy as np\nif np.asarray(data).dtype.kind == 'M':\n    raise TypeError('input is datetime64; compute differences to get timedelta')","typeGuard":"def is_timedelta_convertible(data) -> bool:\n    import numpy as np\n    k = np.asarray(data).dtype.kind\n    return k in 'iufOM' or k == 'm'","tryCatchPattern":"try:\n    s = pd.to_timedelta(data)\nexcept TypeError as e:\n    if 'cannot be converted to timedelta64' in str(e):\n        s = data - np.min(data)  # convert datetime to timedelta via diff\n    else:\n        raise","preventionTips":["Branch on dtype.kind before conversion.","Subtract datetimes to get timedeltas.","Audit column types at ingestion."],"tags":["timedelta","typeerror","conversion","datetime","gh-23539"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}