Class: Polars::LazyGroupBy

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

Overview

Created by df.lazy.group_by("foo").

Instance Method Summary collapse

Instance Method Details

#agg(aggs) ⇒ LazyFrame

Describe the aggregation that need to be done on a group.

Returns:



12
13
14
15
# File 'lib/polars/lazy_group_by.rb', line 12

def agg(aggs)
  rbexprs = Utils.selection_to_rbexpr_list(aggs)
  Utils.wrap_ldf(@lgb.agg(rbexprs))
end

#head(n = 5) ⇒ LazyFrame

Get the first n rows of each group.

Examples:

df = Polars::DataFrame.new(
  {
    "letters" => ["c", "c", "a", "c", "a", "b"],
    "nrs" => [1, 2, 3, 4, 5, 6]
  }
)
df.group_by("letters").head(2).sort("letters")
# =>
# shape: (5, 2)
# ┌─────────┬─────┐
# │ letters ┆ nrs │
# │ ---     ┆ --- │
# │ str     ┆ i64 │
# ╞═════════╪═════╡
# │ a       ┆ 3   │
# │ a       ┆ 5   │
# │ b       ┆ 6   │
# │ c       ┆ 1   │
# │ c       ┆ 2   │
# └─────────┴─────┘

Parameters:

  • n (Integer) (defaults to: 5)

    Number of rows to return.

Returns:



45
46
47
# File 'lib/polars/lazy_group_by.rb', line 45

def head(n = 5)
  Utils.wrap_ldf(@lgb.head(n))
end

#tail(n = 5) ⇒ LazyFrame

Get the last n rows of each group.

Examples:

df = Polars::DataFrame.new(
  {
    "letters" => ["c", "c", "a", "c", "a", "b"],
    "nrs" => [1, 2, 3, 4, 5, 6]
  }
)
df.group_by("letters").tail(2).sort("letters")
# =>
# shape: (5, 2)
# ┌─────────┬─────┐
# │ letters ┆ nrs │
# │ ---     ┆ --- │
# │ str     ┆ i64 │
# ╞═════════╪═════╡
# │ a       ┆ 3   │
# │ a       ┆ 5   │
# │ b       ┆ 6   │
# │ c       ┆ 2   │
# │ c       ┆ 4   │
# └─────────┴─────┘

Parameters:

  • n (Integer) (defaults to: 5)

    Number of rows to return.

Returns:



77
78
79
# File 'lib/polars/lazy_group_by.rb', line 77

def tail(n = 5)
  Utils.wrap_ldf(@lgb.tail(n))
end