Class: Polars::StructExpr

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

Overview

Namespace for struct related expressions.

Instance Method Summary collapse

Instance Method Details

#[](item) ⇒ Expr

Retrieve one of the fields of this Struct as a new Series.

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/polars/struct_expr.rb', line 15

def [](item)
  if item.is_a?(::String)
    field(item)
  elsif item.is_a?(Integer)
    Utils.wrap_expr(_rbexpr.struct_field_by_index(item))
  else
    raise ArgumentError, "expected type Integer or String, got #{item.class.name}"
  end
end

#field(name) ⇒ Expr

Retrieve one of the fields of this Struct as a new Series.

Examples:

df = (
  Polars::DataFrame.new(
    {
      "int" => [1, 2],
      "str" => ["a", "b"],
      "bool" => [true, nil],
      "list" => [[1, 2], [3]]
    }
  )
  .to_struct("my_struct")
  .to_frame
)
df.select(Polars.col("my_struct").struct.field("str"))
# =>
# shape: (2, 1)
# ┌─────┐
# │ str │
# │ --- │
# │ str │
# ╞═════╡
# │ a   │
# │ b   │
# └─────┘

Parameters:

  • name (String)

    Name of the field

Returns:



56
57
58
# File 'lib/polars/struct_expr.rb', line 56

def field(name)
  Utils.wrap_expr(_rbexpr.struct_field_by_name(name))
end

#rename_fields(names) ⇒ Expr

Rename the fields of the struct.

Examples:

df = (
  Polars::DataFrame.new(
    {
      "int" => [1, 2],
      "str" => ["a", "b"],
      "bool" => [true, nil],
      "list" => [[1, 2], [3]]
    }
  )
  .to_struct("my_struct")
  .to_frame
)
df = df.with_column(
  Polars.col("my_struct").struct.rename_fields(["INT", "STR", "BOOL", "LIST"])
)
df.select(Polars.col("my_struct").struct.field("INT"))
# =>
# shape: (2, 1)
# ┌─────┐
# │ INT │
# │ --- │
# │ i64 │
# ╞═════╡
# │ 1   │
# │ 2   │
# └─────┘

Parameters:

  • names (Array)

    New names in the order of the struct's fields

Returns:



94
95
96
# File 'lib/polars/struct_expr.rb', line 94

def rename_fields(names)
  Utils.wrap_expr(_rbexpr.struct_rename_fields(names))
end