pandas-dev/pandas · error · ValueError

Passing in 'datetime64' dtype with no precision is not allow

Error message

Passing in 'datetime64' dtype with no precision is not allowed. Please pass in 'datetime64[ns]' instead.

What it means

Raised by _validate_dt64_dtype when the user passes the bare 'datetime64' (numpy dtype M8 with no unit). Since GH#24806 pandas requires an explicit resolution to avoid platform-dependent unit defaulting. ValueError.

Source

Thrown at pandas/core/arrays/datetimes.py:2984

    Raises
    ------
    ValueError : invalid dtype

    Notes
    -----
    Unlike _validate_tz_from_dtype, this does _not_ allow non-existent
    tz errors to go through
    """
    if dtype is not None:
        dtype = pandas_dtype(dtype)
        if dtype == np.dtype("M8"):
            # no precision, disallowed GH#24806
            msg = (
                "Passing in 'datetime64' dtype with no precision is not allowed. "
                "Please pass in 'datetime64[ns]' instead."
            )
            raise ValueError(msg)

        if (
            isinstance(dtype, np.dtype)
            and (dtype.kind != "M" or not is_supported_dtype(dtype))
        ) or not isinstance(dtype, (np.dtype, DatetimeTZDtype)):
            raise ValueError(
                f"Unexpected value for 'dtype': '{dtype}'. "
                "Must be 'datetime64[s]', 'datetime64[ms]', 'datetime64[us]', "
                "'datetime64[ns]' or DatetimeTZDtype'."
            )

        if getattr(dtype, "tz", None):
            # https://github.com/pandas-dev/pandas/issues/18595
            # Ensure that we have a standard timezone for pytz objects.
            # Without this, things like adding an array of timedeltas and
            # a  tz-aware Timestamp (with a tz specific to its datetime) will
            # be incorrect(ish?) for the array as a whole
            dtype = cast("DatetimeTZDtype", dtype)

View on GitHub (pinned to 71959b8cb9)

Solutions

  1. Pass an explicit resolution: 'datetime64[ns]' (or s/ms/us).
  2. Prefer pandas dtypes ('datetime64[ns]') or DatetimeTZDtype strings for aware data.
  3. Update any helper that builds dtype strings to always include a unit.

Example fix

# before
s.astype('datetime64')
# after
s.astype('datetime64[ns]')
Defensive patterns

Strategy: validation

Validate before calling

import re
SUPPORTED = re.compile(r'^datetime64\[(s|ms|us|ns)\]$')
def validate_dt_dtype(dtype):
    if dtype in ('datetime64', 'M8', np.dtype('M8')):
        raise ValueError("use 'datetime64[ns]' with explicit precision")
    return dtype

Prevention

When it happens

Trigger: pd.DatetimeIndex(data, dtype='datetime64'); Series.astype('datetime64'); np.dtype('datetime64') used as a dtype argument anywhere pandas validates it.

Common situations: Copy-pasted numpy dtype strings; older pandas code written before the precision requirement; dtype inferred from np arrays.

Related errors


AI-assisted analysis of pandas-dev/pandas@71959b8cb9 (2026-08-07). Data as JSON: /api/errors/6565d5a9fc0707c7. Report an issue: GitHub.