{"record":{"id":"d7079e2808034955","repo":"pandas-dev/pandas","slug":"dtype-data-dtype-cannot-be-converted-to-datetime","errorCode":null,"errorMessage":"dtype {data.dtype} cannot be converted to datetime64[ns]","messagePattern":"dtype (.+?) cannot be converted to datetime64\\[ns\\]","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":2903,"sourceCode":"    Raises\n    ------\n    TypeError : PeriodDType data is passed\n    \"\"\"\n    if not hasattr(data, \"dtype\"):\n        # e.g. collections.deque\n        return data, copy\n\n    if is_float_dtype(data.dtype):\n        # pre-2.0 we treated these as wall-times, inconsistent with ints\n        # GH#23675, GH#45573 deprecated to treat symmetrically with integer dtypes.\n        # Note: data.astype(np.int64) fails ARM tests, see\n        # https://github.com/pandas-dev/pandas/issues/49468.\n        data = data.astype(DT64NS_DTYPE).view(\"i8\")\n        copy = False\n\n    elif lib.is_np_dtype(data.dtype, \"m\") or is_bool_dtype(data.dtype):\n        # GH#29794 enforcing deprecation introduced in GH#23539\n        raise TypeError(f\"dtype {data.dtype} cannot be converted to datetime64[ns]\")\n    elif isinstance(data.dtype, PeriodDtype):\n        # Note: without explicitly raising here, PeriodIndex\n        #  test_setops.test_join_does_not_recur fails\n        raise TypeError(\n            \"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead\"\n        )\n\n    elif isinstance(data.dtype, ExtensionDtype) and not isinstance(\n        data.dtype, DatetimeTZDtype\n    ):\n        # TODO: We have no tests for these\n        data = np.array(data, dtype=np.object_)\n        copy = False\n\n    return data, copy\n\n\n# -------------------------------------------------------------------","sourceCodeStart":2885,"sourceCodeEnd":2921,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L2885-L2921","documentation":"Raised by maybe_convert_dtype when the data has a timedelta (m-kind) or boolean dtype and is being fed into a datetime64 path. timedelta and bool values are not datetimes and conversion would be nonsensical, so pandas rejects it. TypeError.","triggerScenarios":"Passing a TimedeltaIndex / boolean Series where a DatetimeIndex is expected, e.g. pd.DatetimeIndex(timedelta_series), or to_datetime on a bool column.","commonSituations":"Wrong column selected; subtracting two dates yields a timedelta that is then mistaken for a datetime; a flag column accidentally routed through datetime parsing.","solutions":["Confirm the column is actually datetime data; select the correct column.","If you have elapsed durations, treat them as timedelta, not datetime.","Convert underlying ints to datetime explicitly only if they represent epoch timestamps, via pd.to_datetime(series, unit='s')."],"exampleFix":"# before\npd.DatetimeIndex(df['elapsed_td'])\n# after\npd.DatetimeIndex(df['event_time'])","handlingStrategy":"type-guard","validationCode":"def to_datetime_column(s):\n    if pd.api.types.is_timedelta64_dtype(s) or pd.api.types.is_bool_dtype(s):\n        raise TypeError(f'column is {s.dtype}, not datetime-like')\n    return pd.to_datetime(s)","typeGuard":"def is_datetime_like(s) -> bool:\n    return pd.api.types.is_datetime64_any_dtype(s) or (\n        s.dtype == object and pd.to_datetime(s, errors='coerce').notna().all()\n    )","tryCatchPattern":null,"preventionTips":["Double-check column selection before datetime construction.","Keep elapsed durations on the timedelta path.","Use unit= for epoch integers."],"tags":["datetime","dtype","timedelta","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}