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

#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