Skip to content

narwhals.Expr.cat

get_categories() -> ExprT

Get unique categories from column.

Returns:

Type Description
ExprT

A new expression.

Examples:

>>> import polars as pl
>>> import narwhals as nw
>>> df_native = pl.DataFrame(
...     {"fruits": ["apple", "mango", "mango"]},
...     schema={"fruits": pl.Categorical},
... )
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("fruits").cat.get_categories()).to_native()
shape: (2, 1)
┌────────┐
│ fruits │
│ ---    │
│ str    │
╞════════╡
│ apple  │
│ mango  │
└────────┘