Skip to content

narwhals.Expr.cat

get_categories

get_categories() -> ExprT

Get unique categories from column.

Warning

This is deprecated and it will be removed in a future version, as Polars removed its own cat.get_categories. To get the distinct values present in a Categorical column, use Expr.unique. For the fixed category list of an Enum, use its dtype.categories.

Until it is removed, it is implemented as unique().drop_nulls().cast(String), so it returns only the values which are actually present, in no guaranteed order.

Note

In narwhals.stable.v1 and narwhals.stable.v2 this stays available, and keeps dispatching to each backend's native implementation.

Examples:

>>> import pandas as pd
>>> import narwhals.stable.v1 as nw
>>> df_native = pd.DataFrame(
...     {"fruits": ["apple", "mango", "mango"]}, dtype="category"
... )
>>> df = nw.from_native(df_native)
>>> df.select(nw.col("fruits").cat.get_categories()).to_native()
  fruits
0  apple
1  mango