pandas-dev/pandas · error · ValueError
dtype ' ' is invalid, should be np.timedelta64 dtype
Error message
dtype '{dtype}' is invalid, should be np.timedelta64 dtype What it means
Raised by _validate_td64_dtype when the resolved dtype is not a numpy timedelta64 dtype at all (after the unit-less 'm8' case is handled separately). This catches inputs like 'int64', 'float64', 'datetime64[ns]', or arbitrary object/category dtypes passed where a timedelta dtype is required.
Solutions
- Pass a valid numpy timedelta64 dtype (e.g. 'timedelta64[ns]').
- If you have integer data, convert it via `pd.to_timedelta(..., unit=...)` instead of forcing a timedelta dtype.
- Validate that `pandas_dtype(dtype).kind == 'm'` before constructing.
Example fix
# before pd.TimedeltaIndex([1, 2, 3], dtype='int64') # ValueError # after pd.TimedeltaIndex([1, 2, 3], dtype='timedelta64[ns]', unit='s')
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
from pandas.api.types import pandas_dtype
def is_valid_td_dtype(dtype) -> bool:
dt = pandas_dtype(dtype)
return dt.kind == 'm' and dt != np.dtype('m8') Type guard
null
Try / catch
try:
idx = pd.TimedeltaIndex(data, dtype=dtype)
except ValueError as e:
if 'should be np.timedelta64' in str(e):
idx = pd.TimedeltaIndex(data, dtype='timedelta64[ns]')
else:
raise Prevention
- Validate dtype.kind == 'm' (with a unit) before constructing timedelta objects.
- Convert integer data via pd.to_timedelta(unit=...) instead of forcing a timedelta dtype.
When it happens
Trigger: `pd.TimedeltaIndex([], dtype='int64')`, or passing a datetime64/category/object dtype string to a timedelta-validated constructor.
Common situations: Reusing a dtype variable intended for a different column; dynamically building dtype strings and supplying the wrong kind.
Related errors
- Passing in 'timedelta' dtype with no precision is not…
- Supported timedelta64 resolutions are 's', 'ms', 'us', 'ns'
- Cannot divide vectors with unequal lengths
- Cannot multiply with
- Cannot multiply with unequal lengths
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/0ef22e6f32ed31a6.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/timedeltas.py:1436
# coerce Index to np.ndarray, converting string-dtype if necessary
values = np.asarray(data, dtype=np.object_)
result = array_to_timedelta64(values, unit=unit, errors=errors)
return result
def _validate_td64_dtype(dtype) -> DtypeObj:
dtype = pandas_dtype(dtype)
if dtype == np.dtype("m8"):
# no precision disallowed GH#24806
msg = (
"Passing in 'timedelta' dtype with no precision is not allowed. "
"Please pass in 'timedelta64[ns]' instead."
)
raise ValueError(msg)
if not lib.is_np_dtype(dtype, "m"):
raise ValueError(f"dtype '{dtype}' is invalid, should be np.timedelta64 dtype")
elif not is_supported_dtype(dtype):
raise ValueError("Supported timedelta64 resolutions are 's', 'ms', 'us', 'ns'")
return dtype
View on GitHub (pinned to 3b7651241d)