Skip to content

narwhals.Series.cat

get_categories

get_categories() -> SeriesT

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 Series.unique. For the fixed category list of an Enum, use its dtype.categories.

Until it is removed, it is implemented as unique(maintain_order=True).drop_nulls().cast(String), so it returns only the values which are actually present, in order of appearance.

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.v2 as nw
>>> s_native = pd.Series(["apple", "mango", "mango"], dtype="category")
>>> s = nw.from_native(s_native, series_only=True)
>>> s.cat.get_categories().to_native()
0    apple
1    mango
dtype: str