Skip to content

narwhals.Expr.cat

get_categories()

Get unique categories from column.

Returns:

Type Description
ExprT

A new expression.

Examples:

Let's create some dataframes:

>>> import pandas as pd
>>> import polars as pl
>>> import narwhals as nw
>>> from narwhals.typing import IntoFrameT
>>> data = {"fruits": ["apple", "mango", "mango"]}
>>> df_pd = pd.DataFrame(data, dtype="category")
>>> df_pl = pl.DataFrame(data, schema={"fruits": pl.Categorical})

We define a dataframe-agnostic function to get unique categories from column 'fruits':

>>> def my_library_agnostic_function(df_native: IntoFrameT) -> IntoFrameT:
...     df = nw.from_native(df_native)
...     return df.select(nw.col("fruits").cat.get_categories()).to_native()

We can then pass either pandas or Polars to func:

>>> my_library_agnostic_function(df_pd)
  fruits
0  apple
1  mango
>>> my_library_agnostic_function(df_pl)
shape: (2, 1)
┌────────┐
│ fruits │
│ ---    │
│ str    │
╞════════╡
│ apple  │
│ mango  │
└────────┘