{"record":{"id":"56abe5bc8b3a1611","repo":"pandas-dev/pandas","slug":"unexpected-value-for-dtype-dtype-must-be","errorCode":null,"errorMessage":"Unexpected value for 'dtype': '{dtype}'. Must be 'datetime64[s]', 'datetime64[ms]', 'datetime64[us]', 'datetime64[ns]' or DatetimeTZDtype'.","messagePattern":"Unexpected value for 'dtype': '(.+?)'\\. Must be 'datetime64\\[s\\]', 'datetime64\\[ms\\]', 'datetime64\\[us\\]', 'datetime64\\[ns\\]' or DatetimeTZDtype'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":2990,"sourceCode":"    -----\n    Unlike _validate_tz_from_dtype, this does _not_ allow non-existent\n    tz errors to go through\n    \"\"\"\n    if dtype is not None:\n        dtype = pandas_dtype(dtype)\n        if dtype == np.dtype(\"M8\"):\n            # no precision, disallowed GH#24806\n            msg = (\n                \"Passing in 'datetime64' dtype with no precision is not allowed. \"\n                \"Please pass in 'datetime64[ns]' instead.\"\n            )\n            raise ValueError(msg)\n\n        if (\n            isinstance(dtype, np.dtype)\n            and (dtype.kind != \"M\" or not is_supported_dtype(dtype))\n        ) or not isinstance(dtype, (np.dtype, DatetimeTZDtype)):\n            raise ValueError(\n                f\"Unexpected value for 'dtype': '{dtype}'. \"\n                \"Must be 'datetime64[s]', 'datetime64[ms]', 'datetime64[us]', \"\n                \"'datetime64[ns]' or DatetimeTZDtype'.\"\n            )\n\n        if getattr(dtype, \"tz\", None):\n            # https://github.com/pandas-dev/pandas/issues/18595\n            # Ensure that we have a standard timezone for pytz objects.\n            # Without this, things like adding an array of timedeltas and\n            # a  tz-aware Timestamp (with a tz specific to its datetime) will\n            # be incorrect(ish?) for the array as a whole\n            dtype = cast(\"DatetimeTZDtype\", dtype)\n            dtype = DatetimeTZDtype(\n                unit=dtype.unit, tz=timezones.tz_standardize(dtype.tz)\n            )\n\n    return dtype\n","sourceCodeStart":2972,"sourceCodeEnd":3008,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L2972-L3008","documentation":"Raised by _validate_dt64_dtype when dtype is a numpy dtype but not a supported datetime resolution (s/ms/us/ns), or is not an np.dtype/DatetimeTZDtype at all. Pandas lists exactly what it accepts and rejects everything else. ValueError.","triggerScenarios":"Passing dtype='datetime64[Y]', dtype='datetime64[D]' (unsupported units), dtype='int64', or a completely unrelated dtype string to a datetime constructor/astype.","commonSituations":"Assuming numpy's 'Y'/'M'/'D' units work (they don't in pandas); passing a numeric dtype to a datetime path; typos in the dtype string.","solutions":["Use one of the supported resolutions: 'datetime64[s]', 'datetime64[ms]', 'datetime64[us]', 'datetime64[ns]'.","For tz-aware data use a DatetimeTZDtype like 'datetime64[ns, Europe/London]'.","If you need year/month/day granularity, build a PeriodIndex instead of a datetime dtype."],"exampleFix":"# before\ns.astype('datetime64[D]')\n# after\ns.astype('datetime64[ns]')","handlingStrategy":"validation","validationCode":"ALLOWED = {'datetime64[s]', 'datetime64[ms]', 'datetime64[us]', 'datetime64[ns]'}\ndef validate_dt_dtype(dtype):\n    if isinstance(dtype, str) and dtype not in ALLOWED and not dtype.startswith('datetime64['):\n        raise ValueError(f\"unsupported datetime dtype: {dtype!r}; use one of {sorted(ALLOWED)}\")\n    return dtype","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Restrict dtype strings to the supported set.","Use PeriodIndex for Y/M/D granularity.","Validate dtype before astype/construction."],"tags":["datetime","dtype","pandas"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}