{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/arrays/categorical.py#L631-L667","documentation":"Raised by Categorical.astype when the categories' underlying dtype cannot be cast to the requested target dtype (the inner `new_cats.astype(dtype)` raised TypeError or ValueError). The error is re-raised with a clearer categorical-specific message pointing at the categories dtype, not the codes.","triggerScenarios":"`cat.astype('int')` where categories are non-numeric strings (e.g. ['a','b']); or `cat.astype(np.float32)` where categories are objects that fail conversion. The fallback block at line 633+ catches the inner cast failure.","commonSituations":"Treating string labels as numbers; converting ordinal labels to numeric codes via astype instead of `.codes`; mismatched dtypes after read_csv type inference.","solutions":["Use `cat.codes` to get integer position encodings instead of astype(int).","Map categories to numeric values explicitly: `cat.map({'a':0,'b':1}).astype(int)`.","Cast to a compatible dtype (e.g. `astype(str)` if categories are strings).","Pre-convert categories: rebuild the Categorical from numeric categories."],"exampleFix":"# before\ncat = pd.Categorical(['a','b','c'])\ncat.astype(int)\n# after\ncat = pd.Categorical(['a','b','c'])\ncat.codes  # integer positions, or use cat.map(mapping)","handlingStrategy":"fallback","validationCode":"import numpy as np\n\ndef cat_cast_or_codes(cat, dtype):\n    try:\n        return cat.astype(dtype)\n    except ValueError:\n        if dtype.kind in 'iu':\n            return cat.codes\n        raise","typeGuard":"def categories_castable_to(cat, dtype) -> bool:\n    try:\n        cat.categories._values.astype(dtype)\n        return True\n    except (TypeError, ValueError):\n        return False","tryCatchPattern":"try:\n    out = cat.astype('int')\nexcept ValueError as e:\n    if 'Cannot cast' in str(e):\n        out = cat.codes\n    else:\n        raise","preventionTips":["Use cat.codes for integer encodings rather than astype(int) on string categories.","Map labels to numbers explicitly with .map(mapping).","Verify categories dtype is numeric before numeric astype."],"tags":["categorical","astype","dtype-cast","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}