pola-rs/polars · error · TypeError

`describe_categorical` only works on categorical columns

Error message

`describe_categorical` only works on categorical columns

What it means

Raised by PolarsColumn.describe_categorical when the column's dtype is neither Categorical nor Enum. The method's contract is to describe categorical metadata (categories, ordering); calling it on any other dtype is a misuse and triggers this error. Check col.dtype / describe_logical first.

Solutions

  1. Only call describe_categorical on columns with the Categorical dtype; cast first with .cast(pl.Categorical) if appropriate.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at py-polars/src/polars/interchange/column.py:78 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/466b9f2a8563e508. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/interchange/column.py:78

    def describe_categorical(self) -> CategoricalDescription:
        """
        Description of the categorical data type of the column.

        Raises
        ------
        TypeError
            If the data type of the column is not categorical.
        """
        dtype = self._col.dtype
        if isinstance(dtype, Categorical):
            categories = self._col.unique().drop_nulls().cast(String)
            is_ordered = False
        elif isinstance(dtype, Enum):
            categories = dtype.categories
            is_ordered = True
        else:
            msg = "`describe_categorical` only works on categorical columns"
            raise TypeError(msg)

        return {
            "is_ordered": is_ordered,
            "is_dictionary": True,
            "categories": PolarsColumn(categories, allow_copy=self._allow_copy),
        }

    @property
    def describe_null(self) -> tuple[ColumnNullType, int | None]:
        """Description of the null representation the column uses."""
        if self.null_count == 0:
            return ColumnNullType.NON_NULLABLE, None
        else:
            return ColumnNullType.USE_BITMASK, 0

    @property
    def null_count(self) -> int:
        """The number of null elements."""

View on GitHub (pinned to 9b5d73fd00)