{"record":{"id":"2bbb3ae0c301e398","repo":"pola-rs/polars","slug":"enum-categories-must-be-strings-found-data-of-typ","errorCode":null,"errorMessage":"Enum categories must be strings; found data of type {dtype}","messagePattern":"Enum categories must be strings; found data of type (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatypes/classes.py","lineNumber":1033,"sourceCode":"\n            enum_values = [\n                getattr(v, \"value\", v) for v in categories.__members__.values()\n            ]\n            categories = pl.Series(values=enum_values)\n        elif not isinstance(categories, pl.Series):\n            categories = pl.Series(values=categories)\n\n        if categories.is_empty():\n            self.categories = pl.Series(name=\"category\", dtype=String)\n            return\n\n        if categories.has_nulls():\n            msg = \"Enum categories must not contain null values\"\n            raise TypeError(msg)\n\n        if (dtype := categories.dtype) != String:\n            msg = f\"Enum categories must be strings; found data of type {dtype}\"\n            raise TypeError(msg)\n\n        if categories.n_unique() != categories.len():\n            duplicate = categories.filter(categories.is_duplicated())[0]\n            msg = f\"Enum categories must be unique; found duplicate {duplicate!r}\"\n            raise ValueError(msg)\n\n        self.categories = categories.rechunk().alias(\"category\")\n\n    def __eq__(self, other: PolarsDataType) -> bool:  # type: ignore[override]\n        # allow comparing object instances to class\n        if type(other) is DataTypeClass and issubclass(other, Enum):\n            return True\n        elif isinstance(other, Enum):\n            return self.categories.equals(other.categories)\n        else:\n            return False\n\n    def __hash__(self) -> int:","sourceCodeStart":1015,"sourceCodeEnd":1051,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatypes/classes.py#L1015-L1051","documentation":"Enum categories must be a Series of pl.String (or convertible iterable of str). If the provided Series has any other dtype (e.g. Int64, Categorical, Date), a TypeError naming the found dtype is raised.","triggerScenarios":"pl.Enum(pl.Series([1, 2, 3])) (integer values); passing a numeric column's unique values as categories; pl.Enum of a Series of datetimes.","commonSituations":"Reusing numeric code columns as enum categories; forgetting that Python ints produce Int64 Series; deriving categories from non-string columns.","solutions":["Cast the Series to String: pl.Enum(categories.cast(pl.String))","Pass a list of Python strings or a String-typed Series","Convert numeric values to strings explicitly before constructing"],"exampleFix":"// before\npl.Enum(pl.Series([1, 2, 3]))\n// after\npl.Enum(pl.Series(['1', '2', '3']))","handlingStrategy":"validation","validationCode":"series = values if isinstance(values, pl.Series) else pl.Series(values)\nif series.dtype != pl.String:\n    series = series.cast(pl.String)\ncategories = pl.Enum(series)","typeGuard":"def is_string_series(s: pl.Series) -> bool:\n    return isinstance(s, pl.Series) and s.dtype == pl.String","tryCatchPattern":"try:\n    dtype = pl.Enum(categories)\nexcept TypeError:\n    dtype = pl.Enum(categories.cast(pl.String))","preventionTips":["Ensure inputs are Python strings before building the Series","Cast to pl.String whenever deriving categories from non-string columns","Document that Enum categories are always strings"],"tags":["polars","enum","dtype","type-error"],"backgroundTag":"dtype-mismatch","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}