{"record":{"id":"0da474184b190a9c","repo":"pandas-dev/pandas","slug":"to-pydatetime-cannot-be-called-with-self-dtype-py","errorCode":null,"errorMessage":"to_pydatetime cannot be called with {self.dtype.pyarrow_dtype} type. Convert to pyarrow timestamp type.","messagePattern":"to_pydatetime cannot be called with (.+?) type\\. Convert to pyarrow timestamp type\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/arrow/array.py","lineNumber":4242,"sourceCode":"        return self._round_temporally(\"round\", freq, ambiguous, nonexistent)\n\n    def _dt_day_name(self, locale: str | None = None) -> Self:\n        if locale is None:\n            locale = \"C\"\n        result = pc.strftime(self._pa_array, format=\"%A\", locale=locale)\n        return self._from_pyarrow_array(result)\n\n    def _dt_month_name(self, locale: str | None = None) -> Self:\n        if locale is None:\n            locale = \"C\"\n        result = pc.strftime(self._pa_array, format=\"%B\", locale=locale)\n        return self._from_pyarrow_array(result)\n\n    def _dt_to_pydatetime(self) -> Series:\n        from pandas import Series\n\n        if pa.types.is_date(self.dtype.pyarrow_dtype):\n            raise ValueError(\n                f\"to_pydatetime cannot be called with {self.dtype.pyarrow_dtype} type. \"\n                \"Convert to pyarrow timestamp type.\"\n            )\n        data = self._pa_array.to_pylist()\n        if self._dtype.pyarrow_dtype.unit == \"ns\":\n            data = [None if ts is None else ts.to_pydatetime(warn=False) for ts in data]\n        return Series(data, dtype=object)\n\n    def _dt_tz_localize(\n        self,\n        tz,\n        ambiguous: TimeAmbiguous = \"raise\",\n        nonexistent: TimeNonexistent = \"raise\",\n    ) -> Self:\n        if ambiguous != \"raise\":\n            raise NotImplementedError(f\"{ambiguous=} is not supported\")\n        nonexistent_pa = {\n            \"raise\": \"raise\",","sourceCodeStart":4224,"sourceCodeEnd":4260,"githubUrl":"https://github.com/pandas-dev/pandas/blob/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/arrow/array.py#L4224-L4260","documentation":"Raised by ArrowExtensionArray._dt_to_pydatetime when the array's pyarrow type is a date type (date32 or date64) rather than a timestamp. to_pydatetime needs wall-clock datetime objects; pyarrow date types lack a time component, so conversion is rejected with ValueError and the user is told to convert to a timestamp type first. Reached through Series.dt.to_pydatetime().","triggerScenarios":"Calling `s.dt.to_pydatetime()` on a Series whose dtype is `date32[pyarrow]` or `date64[pyarrow]`. Common when loading from Parquet/Arrow that stored DATE logical types, or when constructing via pd.array([datetime.date(...)], dtype=...).","commonSituations":"ETL from databases (DATE columns) or Parquet files with date logical type; converting date-only columns to Python datetime objects for downstream APIs.","solutions":["Cast the Series to a timestamp type first: `s.astype(\"timestamp[us][pyarrow]\").dt.to_pydatetime()`.","Use `pd.to_datetime(s)` to get datetime64[ns] then `.dt.to_pydatetime()`.","Operate on the date objects directly via `s.to_numpy()` if you only need date instances.","Confirm dtype with `s.dtype` and convert date->timestamp before calling to_pydatetime."],"exampleFix":"# before\ns = pd.array([datetime.date(2024,1,1)], dtype=\"date32[pyarrow]\")\npd.Series(s).dt.to_pydatetime()  # ValueError\n\n# after\npd.Series(s).astype(\"timestamp[us][pyarrow]\").dt.to_pydatetime()","handlingStrategy":"validation","validationCode":"import pyarrow as pa\n\ndef is_timestamp_pyarrow(s) -> bool:\n    pa_dt = getattr(s.dtype, \"pyarrow_dtype\", None)\n    return pa_dt is not None and pa.types.is_timestamp(pa_dt)\n\ndef safe_to_pydatetime(s):\n    if not is_timestamp_pyarrow(s):\n        s = s.astype(\"timestamp[us][pyarrow]\")\n    return s.dt.to_pydatetime()","typeGuard":"import pyarrow as pa\n\ndef is_timestamp_pyarrow(s) -> bool:\n    pa_dt = getattr(s.dtype, \"pyarrow_dtype\", None)\n    return pa_dt is not None and pa.types.is_timestamp(pa_dt)","tryCatchPattern":"try:\n    return s.dt.to_pydatetime()\nexcept ValueError:\n    return s.astype(\"timestamp[us][pyarrow]\").dt.to_pydatetime()","preventionTips":["Cast date32/date64 pyarrow columns to timestamp[pyarrow] before to_pydatetime.","Inspect dtype after loading Parquet/Arrow; DATE logical types become date32/date64.","Wrap to_pydatetime calls in a helper that normalizes the dtype."],"tags":["pyarrow","datetime-accessor","type-mismatch"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}