{"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":230,"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":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/timedeltas.py#L212-L248","documentation":"Raised by TimedeltaArray._validate_dtype() when the explicitly requested dtype's resolution does not match the resolution of the supplied values array. TimedeltaArray is resolution-aware (s/ms/us/ns), and _simple_new requires values.dtype to equal dtype exactly; a mismatch means the caller asked for one unit while the data carries another, which would silently misinterpret magnitudes. The guard rejects the combination rather than guessing a conversion.","triggerScenarios":"Calling TimedeltaArray._simple_new or _validate_dtype with e.g. values of dtype timedelta64[s] but dtype=timedelta64[ns]. Internal to pandas; surfaces via low-level constructors or extension code that hand-builds a TimedeltaArray.","commonSituations":"Custom extension types wrapping TimedeltaArray; tests that construct arrays with mismatched unit args; bugs in code paths that pass a pre-converted ndarray but a stale dtype.","solutions":["Align the dtype unit with the values: pass dtype=np.dtype('timedelta64[s]') matching values.view('m8[s]').","Convert values to the desired unit first via astype_overflowsafe before constructing.","Use the public TimedeltaIndex/timedelta_range constructors, which handle unit alignment, instead of _simple_new."],"exampleFix":"# before\nvals = np.array([1,2,3], dtype='timedelta64[s]')\nTimedeltaArray._simple_new(vals, dtype=np.dtype('timedelta64[ns]'))  # ValueError\n# after\nTimedeltaArray._simple_new(vals, dtype=vals.dtype)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef build_td_array(values, dtype=None):\n    if dtype is not None and dtype != values.dtype:\n        from pandas._libs.tslibs import astype_overflowsafe\n        values = astype_overflowsafe(values, dtype=dtype, copy=False)\n    return values","typeGuard":"import numpy as np\ndef resolutions_match(values, dtype) -> bool:\n    return dtype is None or dtype == values.dtype","tryCatchPattern":"try:\n    return TimedeltaArray._simple_new(vals, dtype=dtype)\nexcept ValueError as e:\n    if 'resolution does not match' in str(e):\n        return TimedeltaArray._simple_new(vals, dtype=vals.dtype)\n    raise","preventionTips":["Prefer public TimedeltaIndex/timedelta_range over _simple_new.","Always derive dtype from values.dtype, never hardcode a unit.","Add a resolution-consistency assertion in extension wrappers."],"tags":["timedelta","dtype-mismatch","resolution","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}