Method: Arrow::ColumnContainable#select_columns

Defined in:
lib/arrow/column-containable.rb

#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