Class: Charty::TableAdapters::BaseAdapter
- Inherits:
-
Object
- Object
- Charty::TableAdapters::BaseAdapter
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/charty/table_adapters/base_adapter.rb
Direct Known Subclasses
ActiveRecordAdapter, DaruAdapter, DatasetsAdapter, HashAdapter, NArrayAdapter, NMatrixAdapter, PandasDataFrameAdapter
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#index ⇒ Object
Returns the value of attribute index.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #column_names ⇒ Object
- #compare_data_equality(other) ⇒ Object
- #drop_na ⇒ Object
- #group_by(table, grouper, sort, drop_na) ⇒ Object
- #sort_values(by, na_position: :last) ⇒ Object
Instance Attribute Details
#columns ⇒ Object
Returns the value of attribute columns.
9 10 11 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 9 def columns @columns end |
#index ⇒ Object
Returns the value of attribute index.
19 20 21 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 19 def index @index end |
Instance Method Details
#==(other) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 25 def ==(other) case other when BaseAdapter return false if columns != other.columns return false if index != other.index compare_data_equality(other) else false end end |
#column_names ⇒ Object
15 16 17 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 15 def column_names columns.to_a end |
#compare_data_equality(other) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 40 def compare_data_equality(other) columns.each do |name| if self[nil, name] != other[nil, name] return false end end true end |
#drop_na ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 49 def drop_na # TODO: Must implement this method in each adapter missing_index = index.select do |i| column_names.any? do |key| Util.missing?(self[i, key]) end end if missing_index.empty? nil else select_index = index.to_a - missing_index new_data = column_names.map { |key| vals = select_index.map {|i| self[i, key] } [key, vals] }.to_h Charty::Table.new(new_data, index: select_index) end end |
#group_by(table, grouper, sort, drop_na) ⇒ Object
36 37 38 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 36 def group_by(table, grouper, sort, drop_na) Table::HashGroupBy.new(table, grouper, sort, drop_na) end |
#sort_values(by, na_position: :last) ⇒ Object
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/charty/table_adapters/base_adapter.rb', line 68 def sort_values(by, na_position: :last) na_cmp_val = check_na_position(na_position) case by when String, Symbol order = (0 ... length).sort do |i, j| a = self[i, by] b = self[j, by] case when Util.missing?(a) # missing > b na_cmp_val when Util.missing?(b) # a < missing -na_cmp_val else cmp = a <=> b if cmp == 0 i <=> j else cmp end end end when Array order = (0 ... length).sort do |i, j| cmp = 0 by.each do |key| a = self[i, key] b = self[j, key] case when Util.missing?(a) # missing > b cmp = na_cmp_val break when Util.missing?(b) # a < missing cmp = -na_cmp_val break else cmp = a <=> b break if cmp != 0 end end if cmp == 0 i <=> j else cmp end end else raise ArgumentError, "%p is invalid value for `by`" % by end Charty::Table.new( column_names.map { |name| [ name, self[nil, name].values_at(*order) ] }.to_h, index: index.to_a.values_at(*order) ) end |