Module: Arrow::ColumnContainable

Included in:
RecordBatch, Table
Defined in:
lib/arrow/column-containable.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Column #[](index) ⇒ Column #[](range) ⇒ self.class #[](selectors) ⇒ self.class

Overloads:

  • #[](name) ⇒ Column

    Find a column that has the given name.

    Parameters:

    • name (String, Symbol)

      The column name to be found.

    Returns:

    • (Column)

      The found column.

    See Also:

  • #[](index) ⇒ Column

    Find the index-th column.

    Parameters:

    • index (Integer)

      The index to be found.

    Returns:

    • (Column)

      The found column.

    See Also:

  • #[](range) ⇒ self.class

    Selects columns that are in range and creates a new container only with the selected columns.

    Parameters:

    • range (Range)

      The range to be selected.

    Returns:

    • (self.class)

      The newly created container that only has selected columns.

    See Also:

  • #[](selectors) ⇒ self.class

    Selects columns that are selected by selectors and creates a new container only with the selected columns.

    Parameters:

    • selectors (Array)

      The selectors that are used to select columns.

    Returns:

    • (self.class)

      The newly created container that only has selected columns.

    See Also:



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.

Returns:

  • (::Array<String>)

    column names.

Since:

  • 11.0.0



152
153
154
# File 'lib/arrow/column-containable.rb', line 152

def column_names
  @column_names ||= columns.collect(&:name)
end

#columnsObject



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

Overloads:

  • #[](name) ⇒ Column

    Find a column that has the given name.

    Parameters:

    • name (String, Symbol)

      The column name to be found.

    Returns:

    • (Column)

      The found column.

  • #[](index) ⇒ Column

    Find the index-th column.

    Parameters:

    • index (Integer)

      The index to be found.

    Returns:

    • (Column)

      The found 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
    message = "column name or index must be String, Symbol or Integer: "
    message << name_or_index.inspect
    raise ArgumentError, message
  end
end

#merge(other) ⇒ self

Merges columns from the given container or Hash and creates a new container.

Parameters:

  • other (Hash, self)

    The columns to be merged.

Returns:

  • (self)


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
    message = "merge target must be Hash or #{self.class}: " +
      "<#{other.inspect}>: #{inspect}"
    raise ArgumentError, message
  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.

Parameters:

  • selectors (Array<String, Symbol, Integer, Range>)

    If a selector is String, Symbol or Integer, the selector selects a column by #find_column.

    If a selector is Range, the selector selects columns by ::Array#[].

Yields:

  • (column)

    Gives a column to the block to select columns. This uses ::Array#select.

Yield Parameters:

  • column (Column)

    A target column.

Yield Returns:

  • (Boolean)

    Whether the given column is selected or not.

Returns:

  • (self.class)

    The newly created container that only has 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
            message = "unknown column: #{selector.inspect}: #{inspect}"
            raise KeyError.new(message)
          else
            message = "out of index (0..#{n_columns - 1}): "
            message << "#{selector.inspect}: #{inspect}"
            raise IndexError.new(message)
          end
        end
        selected_columns << column
      end
    end
    selected_columns = selected_columns.select(&block) if block_given?
  end
  self.class.new(selected_columns)
end