{"record":{"id":"852f558dccf93a75","repo":"pandas-dev/pandas","slug":"values-resolution-does-not-match-dtype","errorCode":null,"errorMessage":"Values resolution does not match dtype.","messagePattern":"Values resolution does not match dtype\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":293,"sourceCode":"    # ndim is inherited from ExtensionArray, must exist to ensure\n    #  Timestamp.__richcmp__(DateTimeArray) operates pointwise\n\n    # ensure that operations with numpy arrays defer to our implementation\n    __array_priority__ = 1000\n\n    # -----------------------------------------------------------------\n    # Constructors\n\n    _dtype: np.dtype[np.datetime64] | DatetimeTZDtype\n\n    @classmethod\n    def _validate_dtype(cls, values, dtype):\n        # used in TimeLikeOps.__init__\n        dtype = _validate_dt64_dtype(dtype)\n        _validate_dt64_dtype(values.dtype)\n        if isinstance(dtype, np.dtype):\n            if values.dtype != dtype:\n                raise ValueError(\"Values resolution does not match dtype.\")\n        else:\n            vunit = np.datetime_data(values.dtype)[0]\n            if vunit != dtype.unit:\n                raise ValueError(\"Values resolution does not match dtype.\")\n        return dtype\n\n    # error: Signature of \"_simple_new\" incompatible with supertype \"NDArrayBacked\"\n    @classmethod\n    def _simple_new(  # type: ignore[override]\n        cls,\n        values: npt.NDArray[np.datetime64],\n        dtype: np.dtype[np.datetime64] | DatetimeTZDtype = DT64NS_DTYPE,\n    ) -> Self:\n        assert isinstance(values, np.ndarray)\n        assert dtype.kind == \"M\"\n        if isinstance(dtype, np.dtype):\n            assert dtype == values.dtype\n            assert not is_unitless(dtype)","sourceCodeStart":275,"sourceCodeEnd":311,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L275-L311","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Align resolutions explicitly with values = values.astype(f'datetime64[{target_unit}]') or .as_unit(unit) before construction.","Let pandas infer the dtype by passing dtype=None.","Use the public pd.DatetimeIndex / pd.to_datetime constructors, which call as_unit/astype_overflowsafe for you."],"exampleFix":"// before\nvals = np.array(['2020-01-01'], dtype='datetime64[s]')\nDT64NS_DTYPE = np.dtype('datetime64[ns]')\n# internal: _validate_dtype(vals, DT64NS_DTYPE) -> ValueError\n\n// after\nvals = vals.astype('datetime64[ns]')  # or use pd.DatetimeIndex(vals) directly","handlingStrategy":"validation","validationCode":"def align_naive_unit(values, dtype):\n    target = np.dtype(dtype) if isinstance(dtype, str) else dtype\n    if values.dtype != target:\n        values = values.astype(target)\n    return values","typeGuard":"def naive_resolution_matches(values, dtype) -> bool:\n    return values.dtype == dtype","tryCatchPattern":null,"preventionTips":["Prefer public constructors (pd.DatetimeIndex) that handle resolution for you.","When building arrays by hand, always as_unit/astype to the target dtype first.","Avoid mixing datetime64 resolutions across cached buffers."],"tags":["pandas","datetimearray","dtype","resolution","internal"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}