Class: Polars::CatExpr

Inherits:
Object
  • Object
show all
Defined in:
lib/polars/cat_expr.rb

Overview

Namespace for categorical related expressions.

Instance Method Summary collapse

Instance Method Details

#get_categoriesExpr

Get the categories stored in this data type.

Examples:

df = Polars::Series.new(
  "cats", ["foo", "bar", "foo", "foo", "ham"], dtype: Polars::Categorical
).to_frame
df.select(Polars.col("cats").cat.get_categories)
# =>
# shape: (3, 1)
# ┌──────┐
# │ cats │
# │ ---  │
# │ str  │
# ╞══════╡
# │ foo  │
# │ bar  │
# │ ham  │
# └──────┘

Returns:



68
69
70
# File 'lib/polars/cat_expr.rb', line 68

def get_categories
  Utils.wrap_expr(_rbexpr.cat_get_categories)
end

#set_ordering(ordering) ⇒ Expr

Determine how this categorical series should be sorted.

Examples:

df = Polars::DataFrame.new(
  {"cats" => ["z", "z", "k", "a", "b"], "vals" => [3, 1, 2, 2, 3]}
).with_columns(
  [
    Polars.col("cats").cast(:cat).cat.set_ordering("lexical")
  ]
)
df.sort(["cats", "vals"])
# =>
# shape: (5, 2)
# ┌──────┬──────┐
# │ cats ┆ vals │
# │ ---  ┆ ---  │
# │ cat  ┆ i64  │
# ╞══════╪══════╡
# │ a    ┆ 2    │
# │ b    ┆ 3    │
# │ k    ┆ 2    │
# │ z    ┆ 1    │
# │ z    ┆ 3    │
# └──────┴──────┘

Parameters:

  • ordering ("physical", "lexical")

    Ordering type:

    • 'physical' -> Use the physical representation of the categories to determine the order (default).
    • 'lexical' -> Use the string values to determine the ordering.

Returns:



44
45
46
# File 'lib/polars/cat_expr.rb', line 44

def set_ordering(ordering)
  Utils.wrap_expr(_rbexpr.cat_set_ordering(ordering))
end