{"record":{"id":"046d0a6f2e133039","repo":"pola-rs/polars","slug":"enum-categories-must-not-contain-null-values","errorCode":null,"errorMessage":"Enum categories must not contain null values","messagePattern":"Enum categories must not contain null values","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/datatypes/classes.py","lineNumber":1029,"sourceCode":"                if issubclass(categories, enum_subclass):\n                    enum_type_name = categories.__name__\n                    msg = f\"Enum categories must be strings; `{enum_type_name}` values are integers\"\n                    raise TypeError(msg)\n\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)","sourceCodeStart":1011,"sourceCodeEnd":1047,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/datatypes/classes.py#L1011-L1047","documentation":"Enum categories in Polars form the fixed set of allowed string values, so they must not contain nulls. A Series of candidate categories containing any null value is rejected with this TypeError at construction time.","triggerScenarios":"pl.Series([\"a\", None, \"b\"]) passed to pl.Enum(...); building categories from a nullable column via .unique() without dropping nulls; enum classes whose values include None.","commonSituations":"Deriving categories from raw data columns that contain missing values; upstream data with nulls flowing into dtype definitions.","solutions":["Drop nulls first: pl.Enum(series.drop_nulls())","Use .unique().drop_nulls() when deriving categories from a column","Filter None values from the Python list before constructing the Series"],"exampleFix":"// before\npl.Enum(df['status'].unique())\n// after\npl.Enum(df['status'].unique().drop_nulls())","handlingStrategy":"validation","validationCode":"series = pl.Series(values)\nif series.has_nulls():\n    series = series.drop_nulls()\ncategories = pl.Enum(series)","typeGuard":"def is_null_free_string_series(s: pl.Series) -> bool:\n    return s.dtype == pl.String and not s.has_nulls()","tryCatchPattern":"try:\n    dtype = pl.Enum(categories)\nexcept TypeError:\n    dtype = pl.Enum(categories.drop_nulls())","preventionTips":["Always .drop_nulls()/.unique() when deriving categories from data columns","Validate enum-defining input lists for None entries","Keep category definitions as constants rather than data-derived where possible"],"tags":["polars","enum","null","validation"],"backgroundTag":"null-argument","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"}