{"record":{"id":"43e71ff706d0b281","repo":"pandas-dev/pandas","slug":"values-resolution-does-not-match-dtype-43e71f","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/timedeltas.py","lineNumber":247,"sourceCode":"        -------\n        numpy.dtype\n        \"\"\"\n        return self._ndarray.dtype\n\n    @property  # NB: override with cache_readonly in immutable subclasses\n    def _resolution_obj(self) -> Resolution:\n        return get_resolution(self.asi8, tz=None, reso=self._creso)\n\n    # ----------------------------------------------------------------\n    # Constructors\n\n    @classmethod\n    def _validate_dtype(cls, values, dtype):\n        # used in TimeLikeOps.__init__\n        dtype = _validate_td64_dtype(dtype)\n        _validate_td64_dtype(values.dtype)\n        if dtype != values.dtype:\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.timedelta64],\n        dtype: np.dtype[np.timedelta64] = TD64NS_DTYPE,\n    ) -> Self:\n        # Require td64 dtype, not unit-less, matching values.dtype\n        assert lib.is_np_dtype(dtype, \"m\")\n        assert not tslibs.is_unitless(dtype)\n        assert isinstance(values, np.ndarray), type(values)\n        assert dtype == values.dtype\n\n        return super()._simple_new(values=values, dtype=dtype)\n\n    @classmethod","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/timedeltas.py#L229-L265","documentation":"TimedeltaArray._validate_dtype raises ValueError when the requested dtype's resolution (e.g. timedelta64[s]) does not match the resolution of the supplied values array. The array's data and the dtype must agree on time unit to prevent silent rescaling.","triggerScenarios":"Internally constructing a TimedeltaArray with values of dtype timedelta64[ms] but passing dtype=timedelta64('ns'); calling _simple_new or _validate_dtype with mismatched unit parameters.","commonSituations":"Custom subclasses or internal callers passing a stale dtype; building a TimedeltaArray from raw int64 views with one unit while declaring another; misalignment after astype_overflowsafe failures.","solutions":["Ensure the dtype passed matches values.dtype: pass dtype=values.dtype.","Convert the values to the desired unit with astype_overflowsafe before constructing.","Use the public pd.TimedeltaIndex / pd.to_timedelta constructors, which handle resolution matching."],"exampleFix":"// before\narr = np.array([1,2,3], dtype='timedelta64[s]')\nTimedeltaArray._simple_new(arr, dtype=np.dtype('timedelta64[ns]'))  # ValueError\n// after\nTimedeltaArray._simple_new(arr, dtype=arr.dtype)","handlingStrategy":"validation","validationCode":"def build_td_array(values, dtype=None):\n    if dtype is None:\n        dtype = values.dtype\n    if dtype != values.dtype:\n        raise ValueError(f\"dtype {dtype} != values.dtype {values.dtype}\")\n    return dtype","typeGuard":"def td_dtype_matches_values(values, dtype) -> bool:\n    return dtype == values.dtype","tryCatchPattern":"try:\n    TimedeltaArray._simple_new(values, dtype=dtype)\nexcept ValueError as e:\n    if 'resolution does not match' in str(e):\n        TimedeltaArray._simple_new(values, dtype=values.dtype)\n    else:\n        raise","preventionTips":["Pass dtype=values.dtype when constructing TimedeltaArray internals.","Prefer pd.to_timedelta / pd.TimedeltaIndex over internal constructors.","Use astype_overflowsafe to align resolutions before construction."],"tags":["pandas","timedelta","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"}