pandas-dev/pandas · error · ValueError

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

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 supplied dtype is exactly `np.dtype('M8')` — a numpy datetime64 with no unit/precision. Pandas requires an explicit resolution (e.g. `[ns]`, `[s]`) because numpy's unitless datetime64 has implementation-defined behavior. GH#24806.

Solutions

  1. Pass an explicit unit: `'datetime64[ns]'` (or `[s]`, `[ms]`, `[us]`).
  2. If accepting dtype from user input, validate and append a default unit: `dtype = dtype if '[' in dtype else dtype + '[ns]'`.
  3. Prefer `pd.DatetimeIndex` construction without a dtype arg, which defaults to nanoseconds.

Example fix

// before
s = pd.Series(values, dtype='datetime64')

// after
s = pd.Series(values, dtype='datetime64[ns]')
Defensive patterns

Strategy: validation

Validate before calling

import re

def normalize_dt_dtype(s: str) -> str:
    if s in ('datetime64', 'M8', 'datetime64[]'):
        return 'datetime64[ns]'
    return s

Type guard

import re
def has_explicit_unit(dtype_str: str) -> bool:
    return bool(re.fullmatch(r'(datetime64|M8)\[(s|ms|us|ns)(?:,\s*[^\]]+)?\]', dtype_str))

Try / catch

try:
    s = pd.Series(values, dtype=dtype)
except ValueError as e:
    if 'no precision' in str(e):
        s = pd.Series(values, dtype='datetime64[ns]')
    else:
        raise

Prevention

When it happens

Trigger: Passing `dtype='datetime64'`, `dtype=np.dtype('M8')`, or `dtype='M8'` to `pd.DatetimeIndex(...)`, `pd.to_datetime(..., dtype=...)`, `Series.astype('datetime64')`, or any constructor routed through _validate_dt64_dtype.

Common situations: Stale tutorials or code written for older pandas that accepted unitless datetime64. Dynamic dtype strings built by truncating the unit suffix. Copying a dtype from a numpy array's `.dtype` attribute that happens to be unitless.

Related errors


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

Appendix: source

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

    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 3b7651241d)