Method: Polars::Expr#gather

Defined in:
lib/polars/expr.rb

#gather(indices) ⇒ Expr Also known as: take

Take values by index.

Examples:

df = Polars::DataFrame.new(
  {
    "group" => [
      "one",
      "one",
      "one",
      "two",
      "two",
      "two"
    ],
    "value" => [1, 98, 2, 3, 99, 4]
  }
)
df.group_by("group", maintain_order: true).agg(Polars.col("value").take([2, 1]))
# =>
# shape: (2, 2)
# ┌───────┬───────────┐
# │ group ┆ value     │
# │ ---   ┆ ---       │
# │ str   ┆ list[i64] │
# ╞═══════╪═══════════╡
# │ one   ┆ [2, 98]   │
# │ two   ┆ [4, 99]   │
# └───────┴───────────┘

Parameters:

  • indices (Expr)

    An expression that leads to a :u32 dtyped Series.

Returns:

[View source]

1630
1631
1632
1633
1634
1635
1636
1637
# File 'lib/polars/expr.rb', line 1630

def gather(indices)
  if indices.is_a?(::Array)
    indices_lit = Polars.lit(Series.new("", indices, dtype: :u32))._rbexpr
  else
    indices_lit = Utils.parse_into_expression(indices, str_as_lit: false)
  end
  _from_rbexpr(_rbexpr.gather(indices_lit))
end