class TLAW::DataTable

Basically, just a 2-d array with column names. Or you can think of it as an array of hashes. Or loose DataFrame implementation.

Just like this:

“`ruby tbl = DataTable.new([

{id: 1, name: 'Mike', salary: 1000},
{id: 2, name: 'Doris', salary: 900},
{id: 3, name: 'Angie', salary: 1200}

]) # => #<TLAW::DataTable[id, name, salary] x 3> tbl.count # => 3 tbl.keys # => [“id”, “name”, “salary”] tbl # => {“id”=>1, “name”=>“Mike”, “salary”=>1000} tbl # => [1000, 900, 1200] “`

Basically, that's it. Every array of hashes in TLAW response will be converted into corresponding `DataTable`.

Public Class Methods

from_columns(column_names, columns) click to toggle source
# File lib/tlaw/data_table.rb, line 28
def self.from_columns(column_names, columns)
  from_rows(column_names, columns.transpose)
end
from_rows(column_names, rows) click to toggle source
# File lib/tlaw/data_table.rb, line 32
def self.from_rows(column_names, rows)
  new(rows.map { |r| column_names.zip(r).to_h })
end
new(hashes) click to toggle source

Creates DataTable from array of hashes.

Note, that all hash keys are stringified, and all hashes are expanded to have same set of keys.

@param hashes [Array<Hash>]

Calls superclass method
# File lib/tlaw/data_table.rb, line 42
def initialize(hashes)
  hashes = hashes.each_with_index.map { |h, i|
    h.is_a?(Hash) or
      fail ArgumentError,
           "All rows are expected to be hashes, row #{i} is #{h.class}"

    h.map { |k, v| [k.to_s, v] }.to_h
  }
  empty = hashes.map(&:keys).flatten.uniq.map { |k| [k, nil] }.to_h
  hashes = hashes.map { |h| empty.merge(h) }
  super(hashes)
end

Public Instance Methods

[](index_or_column) click to toggle source

Allows access to one column or row.

@overload [](index)

Returns one row from a DataTable.

@param index [Integer] Row number
@return [Hash] Row as a hash

@overload [](column_name)

Returns one column from a DataTable.

@param column_name [String] Name of column
@return [Array] Column as an array of all values in it
Calls superclass method
# File lib/tlaw/data_table.rb, line 76
def [](index_or_column)
  case index_or_column
  when Integer
    super
  when String, Symbol
    map { |h| h[index_or_column.to_s] }
  else
    fail ArgumentError,
         'Expected integer or string/symbol index' \
         ", got #{index_or_column.class}"
  end
end
columns(*names) click to toggle source

Slice of a DataTable with only specified columns left.

@param names [Array<String>] What columns to leave in a DataTable @return [DataTable]

# File lib/tlaw/data_table.rb, line 93
def columns(*names)
  names.map!(&:to_s)
  DataTable.new(map { |h| names.map { |n| [n, h[n]] }.to_h })
end
inspect() click to toggle source

@private

# File lib/tlaw/data_table.rb, line 107
def inspect
  "#<#{self.class.name}[#{keys.join(', ')}] x #{size}>"
end
keys() click to toggle source

All column names.

@return [Array<String>]

# File lib/tlaw/data_table.rb, line 58
def keys
  empty? ? [] : first.keys
end
pretty_print(pp) click to toggle source

@private

# File lib/tlaw/data_table.rb, line 112
def pretty_print(pp)
  pp.text("#<#{self.class.name}[#{keys.join(', ')}] x #{size}>")
end
to_h() click to toggle source

Represents DataTable as a `column name => all values in columns` hash.

@return [Hash{String => Array}]

# File lib/tlaw/data_table.rb, line 102
def to_h
  keys.map { |k| [k, map { |h| h[k] }] }.to_h
end