{"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":"exception","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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L275-L311","documentation":"Raised by DatetimeArray._validate_dtype in the tz-naive (np.dtype) branch when the backing numpy values' datetime64 unit does not equal the declared dtype's unit. pandas must keep the stored array and its dtype in lockstep on resolution; a mismatch (e.g. data stored in 's' but dtype claims 'ns') would corrupt every value silently.","triggerScenarios":"Internal construction path where raw datetime64 arrays of one unit are paired with a dtype of another (e.g. low-level _simple_new misuse, or a custom EA bridge). Not normally user-facing unless bypassing public constructors.","commonSituations":"Custom ExtensionArray subclasses that build DatetimeArray internals; interop code that re-wraps numpy arrays after a unit-changing view; bugs in library bridges (Arrow, Dask) that re-stitch arrays to dtypes.","solutions":["Use astype_overflowsafe to convert the values to the target unit before wrapping, or rely on the public as_unit.","Ensure the dtype passed to the constructor is built from the same unit string as values.dtype (np.datetime_data(values.dtype)[0]).","Reconstruct via cls._simple_new only after both agree; prefer cls._from_sequence / pd.to_datetime for user code."],"exampleFix":"# before\narr = np.array(['2020-01-01'], dtype='datetime64[s]')\ndti = DatetimeArray._simple_new(arr.view('datetime64[ns]'), dtype=DT64NS_DTYPE)\n\n# after\nfrom pandas.core.arrays.datetimes import DatetimeArray\nimport pandas.core.dtypes.common as com\narr = np.array(['2020-01-01'], dtype='datetime64[s]')\nda = DatetimeArray._from_sequence(arr).as_unit('ns')","handlingStrategy":"validation","validationCode":"vunit = np.datetime_data(values.dtype)[0]\nif isinstance(dtype, np.dtype):\n    assert values.dtype == dtype, f'{values.dtype} vs {dtype}'","typeGuard":null,"tryCatchPattern":"try:\n    DatetimeArray._simple_new(values, dtype=dtype)\nexcept ValueError as e:\n    if 'resolution does not match' in str(e):\n        values = astype_overflowsafe(values, dtype)\n        DatetimeArray._simple_new(values, dtype=dtype)\n    else: raise","preventionTips":["Avoid _simple_new in user code; use _from_sequence / pd.to_datetime.","Keep dtype and values unit in sync by deriving one from the other."],"tags":["datetime","dtype","resolution","internal"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}