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
-
#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.
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 |
#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 |