Class: Charty::TableAdapters::HashAdapter
Instance Attribute Summary collapse
Attributes inherited from BaseAdapter
#columns, #index
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from BaseAdapter
#==, #column_names, #drop_na, #group_by, #sort_values
Constructor Details
#initialize(data, columns: nil, index: nil) ⇒ HashAdapter
Returns a new instance of HashAdapter.
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 59
def initialize(data, columns: nil, index: nil)
case data
when Hash
arrays = data.values
columns ||= data.keys
when Array
case data[0]
when Numeric, String, Time, Date
arrays = [data]
when Hash
columns ||= data.map(&:keys).inject(&:|)
arrays = columns.map { [] }
data.each do |record|
columns.each_with_index do |key, i|
arrays[i] << record[key]
end
end
when self.class.method(:array?)
unsupported_data_format unless data.all?(&self.class.method(:array?))
arrays = data.map(&:to_a).transpose
else
unsupported_data_format
end
else
unsupported_data_format
end
unless arrays.empty?
arrays, columns, index = check_data(arrays, columns, index)
end
@data = arrays.map.with_index {|a, i| [columns[i], a] }.to_h
self.columns = columns unless columns.nil?
self.index = index unless index.nil?
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
148
149
150
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 148
def data
@data
end
|
Class Method Details
.array?(data) ⇒ Boolean
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 26
def self.array?(data)
case data
when Charty::Vector
true
when Array, method(:daru_vector?), method(:narray_vector?), method(:nmatrix_vector?),
method(:numpy_vector?), method(:pandas_series?)
true
else
false
end
end
|
.daru_vector?(x) ⇒ Boolean
39
40
41
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 39
def self.daru_vector?(x)
defined?(Daru::Vector) && x.is_a?(Daru::Vector)
end
|
.narray_vector?(x) ⇒ Boolean
43
44
45
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 43
def self.narray_vector?(x)
defined?(Numo::NArray) && x.is_a?(Numo::NArray) && x.ndim == 1
end
|
.nmatrix_vector?(x) ⇒ Boolean
47
48
49
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 47
def self.nmatrix_vector?(x)
defined?(NMatrix) && x.is_a?(NMatrix) && x.dim == 1
end
|
.numpy_vector?(x) ⇒ Boolean
51
52
53
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 51
def self.numpy_vector?(x)
defined?(Numpy::NDArray) && x.is_a?(Numpy::NDArray) && x.ndim == 1
end
|
.pandas_series?(x) ⇒ Boolean
55
56
57
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 55
def self.pandas_series?(x)
defined?(Pandas::Series) && x.is_a?(Pandas::Series)
end
|
.supported?(data) ⇒ Boolean
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 8
def self.supported?(data)
case data
when []
true
when Array
case data[0]
when Numeric, String, Time, Date
true
when Hash
data.all? {|el| el.is_a? Hash }
when method(:array?)
data.all?(&method(:array?))
end
when Hash
true
end
end
|
Instance Method Details
#[](row, column) ⇒ Object
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 174
def [](row, column)
if row
@data[column][row]
else
case column
when Symbol
sym_key = column
str_key = column.to_s
else
str_key = String.try_convert(column)
sym_key = str_key.to_sym
end
column_data = if @data.key?(sym_key)
@data[sym_key]
else
@data[str_key]
end
Vector.new(column_data, index: index, name: column)
end
end
|
#column_length ⇒ Object
161
162
163
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 161
def column_length
data.length
end
|
#compare_data_equality(other) ⇒ Object
165
166
167
168
169
170
171
172
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 165
def compare_data_equality(other)
case other
when DaruAdapter, PandasDataFrameAdapter
other.compare_data_equality(self)
else
super
end
end
|
#each ⇒ Object
196
197
198
199
200
201
202
203
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 196
def each
i, n = 0, shape[0]
while i < n
record = @data.map {|k, v| v[i] }
yield record
i += 1
end
end
|
#length ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 152
def length
case
when column_names.empty?
0
else
data[column_names[0]].size
end
end
|
#reset_index ⇒ Object
205
206
207
208
|
# File 'lib/charty/table_adapters/hash_adapter.rb', line 205
def reset_index
index_name = index.name || :index
Charty::Table.new({ index_name => index.to_a }.merge(data))
end
|