Method: Polars::StructExpr#field

Defined in:
lib/polars/struct_expr.rb

#field(name, *more_names) ⇒ Expr

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

Examples:

df = Polars::DataFrame.new(
  {
    "aaa" => [1, 2],
    "bbb" => ["ab", "cd"],
    "ccc" => [true, nil],
    "ddd" => [[1, 2], [3]]
  }
).select(Polars.struct("aaa", "bbb", "ccc", "ddd").alias("struct_col"))
df.select(Polars.col("struct_col").struct.field("bbb"))
# =>
# shape: (2, 1)
# ┌─────┐
# │ bbb │
# │ --- │
# │ str │
# ╞═════╡
# │ ab  │
# │ cd  │
# └─────┘
df.select(Polars.col("struct_col").struct.field("aaa", "bbb"))
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ aaa ┆ bbb │
# │ --- ┆ --- │
# │ i64 ┆ str │
# ╞═════╪═════╡
# │ 1   ┆ ab  │
# │ 2   ┆ cd  │
# └─────┴─────┘

Parameters:

  • name (String)

    Name of the field

  • more_names (Array)

    Additional struct field names.

Returns:



67
68
69
70
71
72
73
74
75
76
# File 'lib/polars/struct_expr.rb', line 67

def field(name, *more_names)
  if more_names.any?
    name = (name.is_a?(::String) ? [name] : name) + more_names
  end
  if name.is_a?(::Array)
    return Utils.wrap_expr(_rbexpr.struct_multiple_fields(name))
  end

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