pandas-dev/pandas · error · ValueError

Values resolution does not match dtype.

Error message

Values resolution does not match dtype.

What it means

Raised by DatetimeArray._validate_dtype in the tz-naive branch when the supplied numpy values dtype does not exactly equal the requested (validated) datetime64 dtype. Because numpy datetime64 carries its resolution in the dtype string (e.g. datetime64[s] vs datetime64[ns]), a mismatch means the array would be interpreted at the wrong precision; pandas refuses to silently re-interpret.

Solutions

  1. Align resolutions explicitly with values = values.astype(f'datetime64[{target_unit}]') or .as_unit(unit) before construction.
  2. Let pandas infer the dtype by passing dtype=None.
  3. Use the public pd.DatetimeIndex / pd.to_datetime constructors, which call as_unit/astype_overflowsafe for you.

Example fix

// before
vals = np.array(['2020-01-01'], dtype='datetime64[s]')
DT64NS_DTYPE = np.dtype('datetime64[ns]')
# internal: _validate_dtype(vals, DT64NS_DTYPE) -> ValueError

// after
vals = vals.astype('datetime64[ns]')  # or use pd.DatetimeIndex(vals) directly
Defensive patterns

Strategy: validation

Validate before calling

def align_naive_unit(values, dtype):
    target = np.dtype(dtype) if isinstance(dtype, str) else dtype
    if values.dtype != target:
        values = values.astype(target)
    return values

Type guard

def naive_resolution_matches(values, dtype) -> bool:
    return values.dtype == dtype

Prevention

When it happens

Trigger: Internally constructing a DatetimeArray via _simple_new / _validate_dtype where the backing ndarray is datetime64[s] but the dtype argument is datetime64[ns] (or vice versa); hand-built arrays that bypass as_unit.

Common situations: Low-level/pandas-internal code paths; users passing a mis-typed buffer into the constructor; interop with arrow/xarray that hands back a different-resolution datetime64 than requested.

Related errors


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

Appendix: source

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

    # ndim is inherited from ExtensionArray, must exist to ensure
    #  Timestamp.__richcmp__(DateTimeArray) operates pointwise

    # ensure that operations with numpy arrays defer to our implementation
    __array_priority__ = 1000

    # -----------------------------------------------------------------
    # Constructors

    _dtype: np.dtype[np.datetime64] | DatetimeTZDtype

    @classmethod
    def _validate_dtype(cls, values, dtype):
        # used in TimeLikeOps.__init__
        dtype = _validate_dt64_dtype(dtype)
        _validate_dt64_dtype(values.dtype)
        if isinstance(dtype, np.dtype):
            if values.dtype != dtype:
                raise ValueError("Values resolution does not match dtype.")
        else:
            vunit = np.datetime_data(values.dtype)[0]
            if vunit != dtype.unit:
                raise ValueError("Values resolution does not match dtype.")
        return dtype

    # error: Signature of "_simple_new" incompatible with supertype "NDArrayBacked"
    @classmethod
    def _simple_new(  # type: ignore[override]
        cls,
        values: npt.NDArray[np.datetime64],
        dtype: np.dtype[np.datetime64] | DatetimeTZDtype = DT64NS_DTYPE,
    ) -> Self:
        assert isinstance(values, np.ndarray)
        assert dtype.kind == "M"
        if isinstance(dtype, np.dtype):
            assert dtype == values.dtype
            assert not is_unitless(dtype)

View on GitHub (pinned to 3b7651241d)