pandas-dev/pandas · error · TypeError
dtype {data.dtype} cannot be converted to datetime64[ns]
Error message
dtype {data.dtype} cannot be converted to datetime64[ns] What it means
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.
Source
Thrown at pandas/core/arrays/datetimes.py:2903
Raises
------
TypeError : PeriodDType data is passed
"""
if not hasattr(data, "dtype"):
# e.g. collections.deque
return data, copy
if is_float_dtype(data.dtype):
# pre-2.0 we treated these as wall-times, inconsistent with ints
# GH#23675, GH#45573 deprecated to treat symmetrically with integer dtypes.
# Note: data.astype(np.int64) fails ARM tests, see
# https://github.com/pandas-dev/pandas/issues/49468.
data = data.astype(DT64NS_DTYPE).view("i8")
copy = False
elif lib.is_np_dtype(data.dtype, "m") or is_bool_dtype(data.dtype):
# GH#29794 enforcing deprecation introduced in GH#23539
raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]")
elif isinstance(data.dtype, PeriodDtype):
# Note: without explicitly raising here, PeriodIndex
# test_setops.test_join_does_not_recur fails
raise TypeError(
"Passing PeriodDtype data is invalid. Use `data.to_timestamp()` instead"
)
elif isinstance(data.dtype, ExtensionDtype) and not isinstance(
data.dtype, DatetimeTZDtype
):
# TODO: We have no tests for these
data = np.array(data, dtype=np.object_)
copy = False
return data, copy
# -------------------------------------------------------------------View on GitHub (pinned to 71959b8cb9)
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').
Example fix
# before pd.DatetimeIndex(df['elapsed_td']) # after pd.DatetimeIndex(df['event_time'])
Defensive patterns
Strategy: type-guard
Validate before calling
def to_datetime_column(s):
if pd.api.types.is_timedelta64_dtype(s) or pd.api.types.is_bool_dtype(s):
raise TypeError(f'column is {s.dtype}, not datetime-like')
return pd.to_datetime(s) Type guard
def is_datetime_like(s) -> bool:
return pd.api.types.is_datetime64_any_dtype(s) or (
s.dtype == object and pd.to_datetime(s, errors='coerce').notna().all()
) Prevention
- Double-check column selection before datetime construction.
- Keep elapsed durations on the timedelta path.
- Use unit= for epoch integers.
When it happens
Trigger: Passing a TimedeltaIndex / boolean Series where a DatetimeIndex is expected, e.g. pd.DatetimeIndex(timedelta_series), or to_datetime on a bool column.
Common situations: Wrong column selected; subtracting two dates yields a timedelta that is then mistaken for a datetime; a flag column accidentally routed through datetime parsing.
Related errors
- overflow in timedelta operation
- {dtype=} does not have a resolution.
- Passing PeriodDtype data is invalid. Use `data.to_timestamp(
- Passing in 'datetime64' dtype with no precision is not allow
- Unexpected value for 'dtype': '{dtype}'. Must be 'datetime64
AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07).
Data as JSON: /api/errors/d7079e2808034955.
Report an issue: GitHub.