Class: TLAW::DataTable
- Inherits:
-
Array
- Object
- Array
- TLAW::DataTable
- Defined in:
- lib/tlaw/data_table.rb
Overview
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:
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[0]
# => {"id"=>1, "name"=>"Mike", "salary"=>1000}
tbl['salary']
# => [1000, 900, 1200]
Basically, that's it. Every array of hashes in TLAW response will be
converted into corresponding DataTable
.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](index_or_column) ⇒ Object
Allows access to one column or row.
-
#columns(*names) ⇒ DataTable
Slice of a DataTable with only specified columns left.
-
#initialize(hashes) ⇒ DataTable
constructor
Creates DataTable from array of hashes.
-
#keys ⇒ Array<String>
All column names.
-
#to_h ⇒ Hash{String => Array}
Represents DataTable as a
column name => all values in columns
hash.
Constructor Details
#initialize(hashes) ⇒ DataTable
Creates DataTable from array of hashes.
Note, that all hash keys are stringified, and all hashes are expanded to have same set of keys.
42 43 44 45 46 47 48 49 50 51 52 53 |
# 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 |
Class Method Details
.from_columns(column_names, columns) ⇒ Object
28 29 30 |
# 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) ⇒ Object
32 33 34 |
# 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 |
Instance Method Details
#[](index) ⇒ Hash #[](column_name) ⇒ Array
Allows access to one column or row.
76 77 78 79 80 81 82 83 84 85 86 87 |
# 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) ⇒ DataTable
Slice of a DataTable with only specified columns left.
93 94 95 96 |
# 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 |
#keys ⇒ Array<String>
All column names.
58 59 60 |
# File 'lib/tlaw/data_table.rb', line 58 def keys empty? ? [] : first.keys end |
#to_h ⇒ Hash{String => Array}
Represents DataTable as a column name => all values in columns
hash.
102 103 104 |
# File 'lib/tlaw/data_table.rb', line 102 def to_h keys.map { |k| [k, map { |h| h[k] }] }.to_h end |