Class: Polars::Schema

Inherits:
Object
  • Object
show all
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.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/polars/schema.rb', line 8

def initialize(schema = nil, check_dtypes: true)
  input = schema || {}
  @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:



25
26
27
# File 'lib/polars/schema.rb', line 25

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

#[]=(name, dtype) ⇒ Object

Sets the data type of the column.

Returns:



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

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:



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

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)


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

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:



45
46
47
# File 'lib/polars/schema.rb', line 45

def names
  @schema.keys
end

#to_sString Also known as: inspect

Returns a string representing the Schema.

Returns:



76
77
78
# File 'lib/polars/schema.rb', line 76

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