Class: Polars::Schema

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/polars/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema = nil, check_dtypes: true) ⇒ Schema

Ordered mapping of column names to their data type.

Parameters:

  • schema (Object) (defaults to: nil)

    The schema definition given by column names and their associated Polars data type. Accepts a mapping or an enumerable of arrays.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/polars/schema.rb', line 10

def initialize(schema = nil, check_dtypes: true)
  @schema = {}

  if schema.respond_to?(:arrow_c_schema) && !schema.is_a?(Schema)
    Plr.init_polars_schema_from_arrow_c_schema(@schema, schema)
    return
  end

  input = schema || {}
  input.each do |name, tp|
    if !check_dtypes
      @schema[name] = tp
    elsif Utils.is_polars_dtype(tp)
      @schema[name] = _check_dtype(tp)
    else
      self[name] = tp
    end
  end
end

Instance Method Details

#[](key) ⇒ Object

Returns the data type of the column.

Returns:



33
34
35
# File 'lib/polars/schema.rb', line 33

def [](key)
  @schema[key]
end

#[]=(name, dtype) ⇒ Object

Sets the data type of the column.

Returns:



40
41
42
43
# File 'lib/polars/schema.rb', line 40

def []=(name, dtype)
  _check_dtype(dtype)
  @schema[name] = dtype
end

#dtypesArray

Get the data types of the schema.

Examples:

s = Polars::Schema.new({"x" => Polars::UInt8.new, "y" => Polars::List.new(Polars::UInt8)})
s.dtypes
# => [Polars::UInt8, Polars::List(Polars::UInt8)]

Returns:



70
71
72
# File 'lib/polars/schema.rb', line 70

def dtypes
  @schema.values
end

#lengthInteger

Get the number of schema entries.

Examples:

s = Polars::Schema.new({"x" => Polars::Int32.new, "y" => Polars::List.new(Polars::String)})
s.length
# => 2

Returns:

  • (Integer)


82
83
84
# File 'lib/polars/schema.rb', line 82

def length
  @schema.length
end

#namesArray

Get the column names of the schema.

Examples:

s = Polars::Schema.new({"x" => Polars::Float64.new, "y" => Polars::Datetime.new(time_zone: "UTC")})
s.names
# => ["x", "y"]

Returns:



58
59
60
# File 'lib/polars/schema.rb', line 58

def names
  @schema.keys
end

#to_sString Also known as: inspect

Returns a string representing the Schema.

Returns:



89
90
91
# File 'lib/polars/schema.rb', line 89

def to_s
  "#{self.class.name}(#{@schema})"
end