Class: Polars::StructNameSpace

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

Overview

Series.struct 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

#[](item) ⇒ Series

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

Returns:



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

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

#field(name) ⇒ Series

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

Examples:

s = Polars::Series.new([{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}])
s.struct.field("a")
# =>
# shape: (2,)
# Series: 'a' [i64]
# [
#         1
#         3
# ]

Parameters:

  • name (String)

    Name of the field

Returns:



59
60
61
# File 'lib/polars/struct_name_space.rb', line 59

def field(name)
  super
end

#fieldsArray

Get the names of the fields.

Examples:

s = Polars::Series.new([{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}])
s.struct.fields
# => ["a", "b"]

Returns:



34
35
36
37
38
39
40
# File 'lib/polars/struct_name_space.rb', line 34

def fields
  if _s.nil?
    []
  else
    _s.struct_fields
  end
end

#rename_fields(names) ⇒ Series

Rename the fields of the struct.

Examples:

s = Polars::Series.new([{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}])
s.struct.fields
# => ["a", "b"]
s = s.struct.rename_fields(["c", "d"])
s.struct.fields
# => ["c", "d"]

Parameters:

  • names (Array)

    New names in the order of the struct's fields

Returns:



79
80
81
# File 'lib/polars/struct_name_space.rb', line 79

def rename_fields(names)
  super
end

#schemaObject

Get the struct definition as a name/dtype schema dict.

Examples:

s = Polars::Series.new([{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}])
s.struct.schema
# => {"a"=>Polars::Int64, "b"=>Polars::Int64}

Returns:



91
92
93
94
95
96
97
# File 'lib/polars/struct_name_space.rb', line 91

def schema
  if _s.nil?
    {}
  else
    _s.dtype.to_schema
  end
end

#unnestDataFrame

Convert this struct Series to a DataFrame with a separate column for each field.

Examples:

s = Polars::Series.new([{"a" => 1, "b" => 2}, {"a" => 3, "b" => 4}])
s.struct.unnest
# =>
# shape: (2, 2)
# ┌─────┬─────┐
# │ a   ┆ b   │
# │ --- ┆ --- │
# │ i64 ┆ i64 │
# ╞═════╪═════╡
# │ 1   ┆ 2   │
# │ 3   ┆ 4   │
# └─────┴─────┘

Returns:



116
117
118
# File 'lib/polars/struct_name_space.rb', line 116

def unnest
  Utils.wrap_df(_s.struct_unnest)
end