{"record":{"id":"64581e3b8c850f8a","repo":"pandas-dev/pandas","slug":"cannot-cast-self-categories-dtype-dtype-to-dtyp","errorCode":null,"errorMessage":"Cannot cast {self.categories.dtype} dtype to {dtype}","messagePattern":"Cannot cast (.+?) dtype to (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/arrays/categorical.py","lineNumber":649,"sourceCode":"                result = np.array(self, dtype=dtype)\n\n        else:\n            # GH8628 (PERF): astype category codes instead of astyping array\n            new_cats = self.categories._values\n\n            try:\n                new_cats = new_cats.astype(dtype=dtype, copy=copy)\n                fill_value = self.categories._na_value\n                if not is_valid_na_for_dtype(fill_value, dtype):\n                    fill_value = lib.item_from_zerodim(\n                        np.array(self.categories._na_value).astype(dtype)\n                    )\n            except (\n                TypeError,  # downstream error msg for CategoricalIndex is misleading\n                ValueError,\n            ) as err:\n                msg = f\"Cannot cast {self.categories.dtype} dtype to {dtype}\"\n                raise ValueError(msg) from err\n\n            result = take_nd(\n                new_cats, ensure_platform_int(self._codes), fill_value=fill_value\n            )\n\n        return result\n\n    @classmethod\n    def _from_inferred_categories(\n        cls, inferred_categories, inferred_codes, dtype, true_values=None\n    ) -> Self:\n        \"\"\"\n        Construct a Categorical from inferred values.\n\n        For inferred categories (`dtype` is None) the categories are sorted.\n        For explicit `dtype`, the `inferred_categories` are cast to the\n        appropriate type.\n","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/arrays/categorical.py#L631-L667","documentation":"Raised in `Categorical.astype` when casting the underlying category values to the requested `dtype` raises `TypeError` or `ValueError` downstream. The categories themselves cannot be coerced (e.g. non-numeric strings to `int`), so the per-element `take_nd` lookup cannot proceed.","triggerScenarios":"`pd.Categorical(['a', 'b']).astype('int64')`, `pd.Categorical(['1.5', 'x']).astype('float64')`, or any cast where `self.categories.astype(dtype)` fails.","commonSituations":"Assuming a categorical of stringified numbers will coerce cleanly to numeric; leftover sentinel strings (`'NA'`, `'.'`) blocking numeric casts; downstream type narrowing after `read_csv` inferring object columns.","solutions":["Inspect `cat.categories` and clean non-coercible entries before the cast.","Convert via `pd.to_numeric(cat, errors='coerce')` to force unparseable values to NaN.","Cast categories to string first if you want a uniform object/string output: `cat.astype('str')`.","Pre-filter categories to only the numeric-parseable subset."],"exampleFix":"# before\ncat = pd.Categorical(['1', '2', 'x'])\narr = cat.astype('int64')  # ValueError: Cannot cast object dtype to int64\n\n# after\nimport pandas as pd\narr = pd.to_numeric(pd.Series(cat), errors='coerce').to_numpy()","handlingStrategy":"fallback","validationCode":"import pandas as pd\n\ndef safe_numeric_cast(cat):\n    try:\n        return cat.astype('float64')\n    except ValueError:\n        return pd.to_numeric(pd.Series(cat), errors='coerce').to_numpy()","typeGuard":"def categories_are_numeric(cat) -> bool:\n    import pandas as pd\n    return pd.to_numeric(pd.Series(cat.categories), errors='coerce').notna().all()","tryCatchPattern":"try:\n    arr = cat.astype('int64')\nexcept ValueError as e:\n    if 'Cannot cast' in str(e):\n        import pandas as pd\n        arr = pd.to_numeric(pd.Series(cat), errors='coerce').to_numpy()\n    else:\n        raise","preventionTips":["Audit `cat.categories` for non-numeric strings before numeric casts.","Prefer `pd.to_numeric(..., errors='coerce')` for lossy/uncertain conversions.","Clean sentinel strings (`'NA'`, `'.'`) out of the categories first."],"tags":["categorical","astype","cast","valueerror"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}