Class: Polars::CatNameSpace

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

Overview

Series.cat namespace.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Polars::ExprDispatch

Instance Method Details

#get_categoriesSeries

Get the categories stored in this data type.

Examples:

s = Polars::Series.new(["foo", "bar", "foo", "foo", "ham"], dtype: Polars::Categorical)
s.cat.get_categories
# =>
# shape: (3,)
# Series: '' [str]
# [
#         "foo"
#         "bar"
#         "ham"
# ]

Returns:



65
66
67
# File 'lib/polars/cat_name_space.rb', line 65

def get_categories
  super
end

#is_localBoolean

Return whether or not the column is a local categorical.

Examples:

Categoricals constructed without a string cache are considered local.

s = Polars::Series.new(["a", "b", "a"], dtype: Polars::Categorical)
s.cat.is_local
# => true

Categoricals constructed with a string cache are considered global.

s = nil
Polars::StringCache.new do
  s = Polars::Series.new(["a", "b", "a"], dtype: Polars::Categorical)
end
s.cat.is_local
# => false

Returns:



85
86
87
# File 'lib/polars/cat_name_space.rb', line 85

def is_local
  _s.cat_is_local
end

#set_ordering(ordering) ⇒ Series

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:



46
47
48
# File 'lib/polars/cat_name_space.rb', line 46

def set_ordering(ordering)
  super
end

#to_localSeries

Convert a categorical column to its local representation.

This may change the underlying physical representation of the column.

Examples:

Compare the global and local representations of a categorical.

s = nil
Polars::StringCache.new do
  _ = Polars::Series.new("x", ["a", "b", "a"], dtype: Polars::Categorical)
  s = Polars::Series.new("y", ["c", "b", "d"], dtype: Polars::Categorical)
end
s.to_physical
# =>
# shape: (3,)
# Series: 'y' [u32]
# [
#         2
#         1
#         3
# ]
s.cat.to_local.to_physical
# =>
# shape: (3,)
# Series: 'y' [u32]
# [
#         0
#         1
#         2
# ]

Returns:



121
122
123
# File 'lib/polars/cat_name_space.rb', line 121

def to_local
  Utils.wrap_s(_s.cat_to_local)
end