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.

Parameters:

  • name (String)

    Name of the field

Returns:



50
51
52
# File 'lib/polars/struct_name_space.rb', line 50

def field(name)
  super
end

#fieldsArray

Get the names of the fields.

Returns:



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

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

#rename_fields(names) ⇒ Series

Rename the fields of the struct.

Parameters:

  • names (Array)

    New names in the order of the struct's fields

Returns:



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

def rename_fields(names)
  super
end

#schemaObject

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

Returns:



67
68
69
70
71
72
73
# File 'lib/polars/struct_name_space.rb', line 67

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

#to_frameDataFrame

Convert this Struct Series to a DataFrame.

Returns:



29
30
31
# File 'lib/polars/struct_name_space.rb', line 29

def to_frame
  Utils.wrap_df(_s.struct_to_frame)
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:



92
93
94
# File 'lib/polars/struct_name_space.rb', line 92

def unnest
  Utils.wrap_df(_s.struct_unnest)
end