Set Ordering
Description
Determine how this categorical series should be sorted.
Usage
<Expr>$cat$set_ordering(ordering)
Arguments
ordering
|
string either ‘physical’ or ‘lexical’
|
Value
An Expr of datatype Categorical
Examples
library("polars")
df = pl$DataFrame(
cats = factor(c("z", "z", "k", "a", "b")),
vals = c(3, 1, 2, 2, 3)
)
# sort by the string value of categories
df$with_columns(
pl$col("cats")$cat$set_ordering("lexical")
)$sort("cats", "vals")
#> shape: (5, 2)
#> ┌──────┬──────┐
#> │ cats ┆ vals │
#> │ --- ┆ --- │
#> │ cat ┆ f64 │
#> ╞══════╪══════╡
#> │ a ┆ 2.0 │
#> │ b ┆ 3.0 │
#> │ k ┆ 2.0 │
#> │ z ┆ 1.0 │
#> │ z ┆ 3.0 │
#> └──────┴──────┘
# sort by the underlying value of categories
df$with_columns(
pl$col("cats")$cat$set_ordering("physical")
)$sort("cats", "vals")
#> shape: (5, 2)
#> ┌──────┬──────┐
#> │ cats ┆ vals │
#> │ --- ┆ --- │
#> │ cat ┆ f64 │
#> ╞══════╪══════╡
#> │ z ┆ 1.0 │
#> │ z ┆ 3.0 │
#> │ k ┆ 2.0 │
#> │ a ┆ 2.0 │
#> │ b ┆ 3.0 │
#> └──────┴──────┘