Skip to content

Get the categories stored in this data type

Source code

Description

Get the categories stored in this data type

Usage

<Expr>$cat$get_categories()

Value

A polars DataFrame with the categories for each categorical Series.

Examples

library("polars")

df = pl$DataFrame(
  cats = factor(c("z", "z", "k", "a", "b")),
  vals = factor(c(3, 1, 2, 2, 3))
)
df
#> shape: (5, 2)
#> ┌──────┬──────┐
#> │ cats ┆ vals │
#> │ ---  ┆ ---  │
#> │ cat  ┆ cat  │
#> ╞══════╪══════╡
#> │ z    ┆ 3    │
#> │ z    ┆ 1    │
#> │ k    ┆ 2    │
#> │ a    ┆ 2    │
#> │ b    ┆ 3    │
#> └──────┴──────┘
df$select(
  pl$col("cats")$cat$get_categories()
)
#> shape: (4, 1)
#> ┌──────┐
#> │ cats │
#> │ ---  │
#> │ str  │
#> ╞══════╡
#> │ z    │
#> │ k    │
#> │ a    │
#> │ b    │
#> └──────┘
df$select(
  pl$col("vals")$cat$get_categories()
)
#> shape: (3, 1)
#> ┌──────┐
#> │ vals │
#> │ ---  │
#> │ str  │
#> ╞══════╡
#> │ 3    │
#> │ 1    │
#> │ 2    │
#> └──────┘