Module: Arrow::ColumnContainable
- Included in:
- RecordBatch, Table
- Defined in:
- lib/arrow/column-containable.rb
Instance Method Summary collapse
- #[](selector) ⇒ Object
-
#column_names ⇒ ::Array<String>
Return column names in this object.
- #columns ⇒ Object
- #each_column(&block) ⇒ Object
- #find_column(name_or_index) ⇒ Object
-
#merge(other) ⇒ self
Merges columns from the given container or Hash and creates a new container.
-
#select_columns(*selectors) {|column| ... } ⇒ self.class
Selects columns that are selected by
selectorsand/orblockand creates a new container only with the selected columns.
Instance Method Details
#[](name) ⇒ Column #[](index) ⇒ Column #[](range) ⇒ self.class #[](selectors) ⇒ self.class
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/arrow/column-containable.rb', line 136 def [](selector) case selector when ::Array select_columns(*selector) when Range select_columns(selector) else find_column(selector) end end |
#column_names ⇒ ::Array<String>
Return column names in this object.
152 153 154 |
# File 'lib/arrow/column-containable.rb', line 152 def column_names @column_names ||= columns.collect(&:name) end |
#columns ⇒ Object
20 21 22 23 24 |
# File 'lib/arrow/column-containable.rb', line 20 def columns @columns ||= schema.n_fields.times.collect do |i| Column.new(self, i) end end |
#each_column(&block) ⇒ Object
26 27 28 |
# File 'lib/arrow/column-containable.rb', line 26 def each_column(&block) columns.each(&block) end |
#[](name) ⇒ Column #[](index) ⇒ Column
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/arrow/column-containable.rb', line 41 def find_column(name_or_index) case name_or_index when String, Symbol name = name_or_index.to_s index = schema.get_field_index(name) return nil if index == -1 Column.new(self, index) when Integer index = name_or_index index += n_columns if index < 0 return nil if index < 0 or index >= n_columns Column.new(self, index) else = "column name or index must be String, Symbol or Integer: " << name_or_index.inspect raise ArgumentError, end end |
#merge(other) ⇒ self
Merges columns from the given container or Hash and creates a new container.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/arrow/column-containable.rb', line 163 def merge(other) added_columns = {} removed_columns = {} case other when Hash other.each do |name, value| name = name.to_s if value added_columns[name] = ensure_raw_column(name, value) else removed_columns[name] = true end end when self.class other.columns.each do |column| name = column.name added_columns[name] = ensure_raw_column(name, column) end else = "merge target must be Hash or #{self.class}: " + "<#{other.inspect}>: #{inspect}" raise ArgumentError, end new_columns = [] columns.each do |column| column_name = column.name new_column = added_columns.delete(column_name) if new_column new_columns << new_column next end next if removed_columns.key?(column_name) new_columns << ensure_raw_column(column_name, column) end added_columns.each_value do |new_column| new_columns << new_column end new_fields = [] new_arrays = [] new_columns.each do |new_column| new_fields << new_column[:field] new_arrays << new_column[:data] end merged = self.class.new(new_fields, new_arrays) share_input(merged) merged end |
#select_columns(*selectors) {|column| ... } ⇒ self.class
Selects columns that are selected by selectors and/or block
and creates a new container only with the selected columns.
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 |
# File 'lib/arrow/column-containable.rb', line 74 def select_columns(*selectors, &block) if selectors.empty? return to_enum(__method__) unless block_given? selected_columns = columns.select(&block) else selected_columns = [] selectors.each do |selector| case selector when Range selected_columns.concat(columns[selector]) else column = find_column(selector) if column.nil? case selector when String, Symbol = "unknown column: #{selector.inspect}: #{inspect}" raise KeyError.new() else = "out of index (0..#{n_columns - 1}): " << "#{selector.inspect}: #{inspect}" raise IndexError.new() end end selected_columns << column end end selected_columns = selected_columns.select(&block) if block_given? end self.class.new(selected_columns) end |