{"record":{"id":"6565d5a9fc0707c7","repo":"pandas-dev/pandas","slug":"passing-in-datetime64-dtype-with-no-precision-is","errorCode":null,"errorMessage":"Passing in 'datetime64' dtype with no precision is not allowed. Please pass in 'datetime64[ns]' instead.","messagePattern":"Passing in 'datetime64' dtype with no precision is not allowed\\. Please pass in 'datetime64\\[ns\\]' instead\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":2982,"sourceCode":"\n    Raises\n    ------\n    ValueError : invalid dtype\n\n    Notes\n    -----\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)","sourceCodeStart":2964,"sourceCodeEnd":3000,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/datetimes.py#L2964-L3000","documentation":"Raised by _validate_dt64_dtype when the supplied dtype is exactly `np.dtype('M8')` — a numpy datetime64 with no unit/precision. Pandas requires an explicit resolution (e.g. `[ns]`, `[s]`) because numpy's unitless datetime64 has implementation-defined behavior. GH#24806.","triggerScenarios":"Passing `dtype='datetime64'`, `dtype=np.dtype('M8')`, or `dtype='M8'` to `pd.DatetimeIndex(...)`, `pd.to_datetime(..., dtype=...)`, `Series.astype('datetime64')`, or any constructor routed through _validate_dt64_dtype.","commonSituations":"Stale tutorials or code written for older pandas that accepted unitless datetime64. Dynamic dtype strings built by truncating the unit suffix. Copying a dtype from a numpy array's `.dtype` attribute that happens to be unitless.","solutions":["Pass an explicit unit: `'datetime64[ns]'` (or `[s]`, `[ms]`, `[us]`).","If accepting dtype from user input, validate and append a default unit: `dtype = dtype if '[' in dtype else dtype + '[ns]'`.","Prefer `pd.DatetimeIndex` construction without a dtype arg, which defaults to nanoseconds."],"exampleFix":"// before\ns = pd.Series(values, dtype='datetime64')\n\n// after\ns = pd.Series(values, dtype='datetime64[ns]')","handlingStrategy":"validation","validationCode":"import re\n\ndef normalize_dt_dtype(s: str) -> str:\n    if s in ('datetime64', 'M8', 'datetime64[]'):\n        return 'datetime64[ns]'\n    return s","typeGuard":"import re\ndef has_explicit_unit(dtype_str: str) -> bool:\n    return bool(re.fullmatch(r'(datetime64|M8)\\[(s|ms|us|ns)(?:,\\s*[^\\]]+)?\\]', dtype_str))","tryCatchPattern":"try:\n    s = pd.Series(values, dtype=dtype)\nexcept ValueError as e:\n    if 'no precision' in str(e):\n        s = pd.Series(values, dtype='datetime64[ns]')\n    else:\n        raise","preventionTips":["Never pass 'datetime64' without a unit suffix.","Validate dtype strings from config/user input before forwarding to pandas."],"tags":["datetime","dtype","unit-resolution","construction"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}