pandas-dev/pandas · error · ValueError
Passing in 'timedelta' dtype with no precision is not…
Error message
Passing in 'timedelta' dtype with no precision is not allowed. Please pass in 'timedelta64[ns]' instead.
What it means
Raised by _validate_td64_dtype when the supplied dtype is the unit-less `timedelta64` (numpy dtype 'm8', no precision). Pandas requires an explicit resolution (GH#24806); a bare timedelta dtype is ambiguous because the backing nanosecond representation needs a defined unit. The message directs the caller to use 'timedelta64[ns]'.
Solutions
- Specify a resolution: use 'timedelta64[ns]' (or 's','ms','us' as supported).
- Omit the dtype entirely and let pandas infer it from timedelta data.
- Validate dtype strings before passing them to constructors.
Example fix
# before pd.Index(pd.to_timedelta([1, 2], unit='D'), dtype='timedelta') # ValueError # after pd.Index(pd.to_timedelta([1, 2], unit='D'), dtype='timedelta64[ns]')
Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def validate_td_dtype_str(dtype_str: str) -> bool:
try:
dt = np.dtype(dtype_str)
except TypeError:
return False
return dt != np.dtype('m8') and dt.kind == 'm' Type guard
null
Try / catch
try:
idx = pd.TimedeltaIndex(data, dtype=dtype)
except ValueError as e:
if 'no precision' in str(e):
idx = pd.TimedeltaIndex(data, dtype='timedelta64[ns]')
else:
raise Prevention
- Always specify a resolution (e.g. 'timedelta64[ns]').
- Omit dtype and let pandas infer from timedelta data when unsure.
When it happens
Trigger: Constructing a TimedeltaIndex/Index with dtype='timedelta' or dtype='timedelta64' (no unit), e.g. `pd.Index([], dtype='timedelta')`.
Common situations: Copying a loose dtype string from documentation or older code; building dtype dynamically without appending a unit.
Related errors
- dtype ' ' is invalid, should be np.timedelta64 dtype
- Supported timedelta64 resolutions are 's', 'ms', 'us', 'ns'
- Cannot convert input with unit
- Cannot divide vectors with unequal lengths
- Cannot multiply with
AI-assisted analysis of pandas-dev/pandas@3b7651241d (2026-08-11).
Data as JSON: /api/errors/645da3b018f50624.
Report an issue: GitHub.
Appendix: source
Thrown at pandas/core/arrays/timedeltas.py:1433
errors to be ignored; they are caught and subsequently ignored at a
higher level.
"""
# 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)