{"record":{"id":"22093928d8f5b8d6","repo":"pandas-dev/pandas","slug":"casting-to-unit-less-dtype-datetime64-is-not-sup","errorCode":null,"errorMessage":"Casting to unit-less dtype 'datetime64' is not supported. Pass e.g. 'datetime64[ns]' instead.","messagePattern":"Casting to unit-less dtype 'datetime64' is not supported\\. Pass e\\.g\\. 'datetime64\\[ns\\]' instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/datetimes.py","lineNumber":743,"sourceCode":"            # TODO: preserve freq?\n\n        elif self.tz is not None and lib.is_np_dtype(dtype, \"M\"):\n            # pre-2.0 behavior for DTA/DTI was\n            #  values.tz_convert(\"UTC\").tz_localize(None), which did not match\n            #  the Series behavior\n            raise TypeError(\n                \"Cannot use .astype to convert from timezone-aware dtype to \"\n                \"timezone-naive dtype. Use obj.tz_localize(None) or \"\n                \"obj.tz_convert('UTC').tz_localize(None) instead.\"\n            )\n\n        elif (\n            self.tz is None\n            and lib.is_np_dtype(dtype, \"M\")\n            and dtype != self.dtype\n            and is_unitless(dtype)\n        ):\n            raise TypeError(\n                \"Casting to unit-less dtype 'datetime64' is not supported. \"\n                \"Pass e.g. 'datetime64[ns]' instead.\"\n            )\n\n        elif isinstance(dtype, PeriodDtype):\n            return self.to_period(freq=dtype.freq)\n        return dtl.DatetimeLikeArrayMixin.astype(self, dtype, copy)\n\n    # -----------------------------------------------------------------\n    # Rendering Methods\n\n    def _format_native_types(\n        self, *, na_rep: str | float = \"NaT\", date_format=None, **kwargs\n    ) -> npt.NDArray[np.object_]:\n        if date_format is None and self._is_dates_only:\n            # Only dates and no timezone: provide a default format\n            date_format = \"%Y-%m-%d\"\n","sourceCodeStart":725,"sourceCodeEnd":761,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/datetimes.py#L725-L761","documentation":"Raised by DatetimeArray.astype when the target is a unit-less numpy datetime64 (e.g. np.dtype('datetime64') or the string 'datetime64'). Unit-less datetime64 is legacy numpy and pandas requires every datetime storage to declare a resolution ('s','ms','us','ns'); accepting the bare form would leave the unit ambiguous and pick a default silently.","triggerScenarios":"s.astype('datetime64'); idx.astype(np.dtype('datetime64')); df['ts'].astype('datetime64') where ts is tz-naive and dtype != self.dtype.","commonSituations":"Old numpy idioms (np.dtype('datetime64')); tutorials/StackOverflow snippets using the bare form; config files that store dtype strings without a unit suffix.","solutions":["Specify a unit: s.astype('datetime64[ns]') (or 's','ms','us').","For tz-aware data, strip the tz first (tz_localize/tz_convert) then astype to a unit-ful datetime64.","If you just want a unit change, use obj.as_unit('s')."],"exampleFix":"# before\ns.astype('datetime64')\n\n# after\ns.astype('datetime64[ns]')","handlingStrategy":"validation","validationCode":"if isinstance(target, str) and target == 'datetime64':\n    raise ValueError(\"specify a unit: 'datetime64[ns]' (or s/ms/us)\")\nif isinstance(target, np.dtype) and target == np.dtype('datetime64'):\n    raise ValueError('unit-less datetime64 not allowed; use datetime64[ns]')","typeGuard":"def is_unitless_dt64(t) -> bool:\n    try:\n        return np.dtype(t).name == 'datetime64'  # no [unit]\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    s.astype(target)\nexcept TypeError as e:\n    if 'unit-less dtype' in str(e):\n        s.astype('datetime64[ns]')\n    else: raise","preventionTips":["Always include a unit suffix in datetime dtype strings.","Reject bare 'datetime64' / np.dtype('datetime64') at the config boundary."],"tags":["datetime","astype","unit","numpy","type-error"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}