Class: Arrow::Table

Inherits:
Object
  • Object
show all
Includes:
ColumnContainable, RecordContainable
Defined in:
lib/arrow/table.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RecordContainable

#each_record

Methods included from ColumnContainable

#columns, #each_column, #find_column

Constructor Details

#initialize(columns) ⇒ Table #initialize(raw_table) ⇒ Table #initialize(raw_table) ⇒ Table #initialize(raw_table) ⇒ Table #initialize(schema, columns) ⇒ Table #initialize(schema, arrays) ⇒ Table #initialize(schema, record_batches) ⇒ Table #initialize(schema, raw_records) ⇒ Table

Creates a new Arrow::Table.

Overloads:

  • #initialize(columns) ⇒ Table

    Examples:

    Create a table from columns

    count_field = Arrow::Field.new("count", :uint32)
    count_array = Arrow::UInt32Array.new([0, 2, nil, 4])
    count_column = Arrow::Column.new(count_field, count_array)
    visible_field = Arrow::Field.new("visible", :boolean)
    visible_array = Arrow::BooleanArray.new([true, nil, nil, false])
    visible_column = Arrow::Column.new(visible_field, visible_array)
    Arrow::Table.new([count_column, visible_column])

    Parameters:

  • #initialize(raw_table) ⇒ Table

    Examples:

    Create a table from column name and values

    Arrow::Table.new("count" => Arrow::UInt32Array.new([0, 2, nil, 4]),
                     "visible" => Arrow::BooleanArray.new([true, nil, nil, false]))

    Parameters:

    • raw_table (Hash<String, Arrow::Array>)

      The pairs of column name and values of the table. Column values is ‘Arrow::Array`.

  • #initialize(raw_table) ⇒ Table

    Examples:

    Create a table from column name and values

    count_chunks = [
      Arrow::UInt32Array.new([0, 2]),
      Arrow::UInt32Array.new([nil, 4]),
    ]
    visible_chunks = [
      Arrow::BooleanArray.new([true]),
      Arrow::BooleanArray.new([nil, nil, false]),
    ]
    Arrow::Table.new("count" => Arrow::ChunkedArray.new(count_chunks),
                     "visible" => Arrow::ChunkedArray.new(visible_chunks))

    Parameters:

    • raw_table (Hash<String, Arrow::ChunkedArray>)

      The pairs of column name and values of the table. Column values is ‘Arrow::ChunkedArray`.

  • #initialize(raw_table) ⇒ Table

    Examples:

    Create a table from column name and values

    count_chunks = [
      Arrow::UInt32Array.new([0, 2]),
      Arrow::UInt32Array.new([nil, 4]),
    ]
    visible_chunks = [
      Arrow::BooleanArray.new([true]),
      Arrow::BooleanArray.new([nil, nil, false]),
    ]
    Arrow::Table.new("count" => [0, 2, nil, 4],
                     "visible" => [true, nil, nil, false])

    Parameters:

    • raw_table (Hash<String, ::Array>)

      The pairs of column name and values of the table. Column values is ‘Array`.

  • #initialize(schema, columns) ⇒ Table

    Examples:

    Create a table from schema and columns

    count_field = Arrow::Field.new("count", :uint32)
    count_array = Arrow::UInt32Array.new([0, 2, nil, 4])
    count_column = Arrow::Column.new(count_field, count_array)
    visible_field = Arrow::Field.new("visible", :boolean)
    visible_array = Arrow::BooleanArray.new([true, nil, nil, false])
    visible_column = Arrow::Column.new(visible_field, visible_array)
    Arrow::Table.new(Arrow::Schema.new([count_field, visible_field]),
                     [count_column, visible_column])

    Parameters:

  • #initialize(schema, arrays) ⇒ Table

    Examples:

    Create a table from schema and arrays

    count_field = Arrow::Field.new("count", :uint32)
    count_array = Arrow::UInt32Array.new([0, 2, nil, 4])
    visible_field = Arrow::Field.new("visible", :boolean)
    visible_array = Arrow::BooleanArray.new([true, nil, nil, false])
    Arrow::Table.new(Arrow::Schema.new([count_field, visible_field]),
                     [count_array, visible_array])

    Parameters:

  • #initialize(schema, record_batches) ⇒ Table

    Examples:

    Create a table from schema and record batches

    count_field = Arrow::Field.new("count", :uint32)
    visible_field = Arrow::Field.new("visible", :boolean)
    schema = Arrow::Schema.new([count_field, visible_field])
    record_batches = [
      Arrow::RecordBatch.new(schema, [[0, true], [2, nil], [nil, nil]]),
      Arrow::RecordBatch.new(schema, [[4, false]]),
    ]
    Arrow::Table.new(schema, record_batches)

    Parameters:

  • #initialize(schema, raw_records) ⇒ Table

    Examples:

    Create a table from schema and raw records

    schema = {
      count: :uint32,
      visible: :boolean,
    }
    raw_records = [
      [0, true],
      [2, nil],
      [nil, nil],
      [4, false],
    ]
    Arrow::Table.new(schema, raw_records)

    Parameters:

    • schema (Arrow::Schema)

      The schema of the table. You can also specify schema as primitive Ruby objects. See Schema#initialize for details.

    • arrays (::Array<::Array>)

      The data of the table as primitive Ruby objects.



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
# File 'lib/arrow/table.rb', line 170

def initialize(*args)
  n_args = args.size
  case n_args
  when 1
    if args[0][0].is_a?(Column)
      columns = args[0]
      fields = columns.collect(&:field)
      values = columns.collect(&:data)
      schema = Schema.new(fields)
    else
      raw_table = args[0]
      fields = []
      values = []
      raw_table.each do |name, array|
        array = ArrayBuilder.build(array) if array.is_a?(::Array)
        fields << Field.new(name.to_s, array.value_data_type)
        values << array
      end
      schema = Schema.new(fields)
    end
  when 2
    schema = args[0]
    schema = Schema.new(schema) unless schema.is_a?(Schema)
    values = args[1]
    case values[0]
    when ::Array
      values = [RecordBatch.new(schema, values)]
    when Column
      values = values.collect(&:data)
    end
  else
    message = "wrong number of arguments (given #{n_args}, expected 1..2)"
    raise ArgumentError, message
  end
  initialize_raw(schema, values)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



508
509
510
511
512
513
514
# File 'lib/arrow/table.rb', line 508

def method_missing(name, *args, &block)
  if args.empty?
    column = find_column(name)
    return column if column
  end
  super
end

Class Method Details

.load(path, options = {}) ⇒ Object



28
29
30
# File 'lib/arrow/table.rb', line 28

def load(path, options={})
  TableLoader.load(path, options)
end

Instance Method Details

#each_record_batchObject



207
208
209
210
211
212
213
214
# File 'lib/arrow/table.rb', line 207

def each_record_batch
  return to_enum(__method__) unless block_given?

  reader = TableBatchReader.new(self)
  while record_batch = reader.read_next
    yield(record_batch)
  end
end

#group(*keys) ⇒ Object

Experimental



459
460
461
# File 'lib/arrow/table.rb', line 459

def group(*keys)
  Group.new(self, keys)
end

#inspectObject



499
500
501
# File 'lib/arrow/table.rb', line 499

def inspect
  "#{super}\n#{to_s}"
end

#inspect_rawObject



498
# File 'lib/arrow/table.rb', line 498

alias_method :inspect_raw, :inspect

#merge(other) ⇒ Arrow::Table

TODO

Returns:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/arrow/table.rb', line 352

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 Table
    added_columns = {}
    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 Arrow::Table: " +
      "<#{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 do |name, 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
  self.class.new(new_fields, new_arrays)
end

#packObject



473
474
475
476
477
478
# File 'lib/arrow/table.rb', line 473

def pack
  packed_arrays = columns.collect do |column|
    column.data.pack
  end
  self.class.new(schema, packed_arrays)
end

#remove_column(name_or_index) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/arrow/table.rb', line 402

def remove_column(name_or_index)
  case name_or_index
  when String, Symbol
    name = name_or_index.to_s
    index = columns.index {|column| column.name == name}
    if index.nil?
      message = "unknown column: #{name_or_index.inspect}: #{inspect}"
      raise KeyError.new(message)
    end
  else
    index = name_or_index
    index += n_columns if index < 0
    if index < 0 or index >= n_columns
      message = "out of index (0..#{n_columns - 1}): " +
        "#{name_or_index.inspect}: #{inspect}"
      raise IndexError.new(message)
    end
  end
  remove_column_raw(index)
end

#remove_column_rawObject



401
# File 'lib/arrow/table.rb', line 401

alias_method :remove_column_raw, :remove_column

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


503
504
505
506
# File 'lib/arrow/table.rb', line 503

def respond_to_missing?(name, include_private)
  return true if find_column(name)
  super
end

#save(path, options = {}) ⇒ Object



468
469
470
471
# File 'lib/arrow/table.rb', line 468

def save(path, options={})
  saver = TableSaver.new(self, path, options)
  saver.save
end

#select_columns(*selectors, &block) ⇒ Arrow::Table

TODO

Returns:



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/arrow/table.rb', line 426

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 String, Symbol
        column = find_column(selector)
        if column.nil?
          message = "unknown column: #{selector.inspect}: #{inspect}"
          raise KeyError.new(message)
        end
        selected_columns << column
      when Range
        selected_columns.concat(columns[selector])
      else
        column = columns[selector]
        if column.nil?
          message = "out of index (0..#{n_columns - 1}): " +
          "#{selector.inspect}: #{inspect}"
          raise IndexError.new(message)
        end
        selected_columns << column
      end
    end
    selected_columns = selected_columns.select(&block) if block_given?
  end
  self.class.new(selected_columns)
end

#slice(offset, length) ⇒ Arrow::Table #slice(index) ⇒ Arrow::Record #slice(booleans) ⇒ Arrow::Table #slice(boolean_array) ⇒ Arrow::Table #slice(range) ⇒ Arrow::Table #slice {|slicer| ... } ⇒ Arrow::Table

Overloads:

  • #slice(offset, length) ⇒ Arrow::Table

    Returns The sub ‘Arrow::Table` that covers only from `offset` to `offset + length` range.

    Parameters:

    • offset (Integer)

      The offset of sub Arrow::Table.

    • length (Integer)

      The length of sub Arrow::Table.

    Returns:

    • (Arrow::Table)

      The sub ‘Arrow::Table` that covers only from `offset` to `offset + length` range.

  • #slice(index) ⇒ Arrow::Record

    Returns The ‘Arrow::Record` corresponding to index of the table.

    Parameters:

    • index (Integer)

      The index in this table.

    Returns:

    • (Arrow::Record)

      The ‘Arrow::Record` corresponding to index of the table.

  • #slice(booleans) ⇒ Arrow::Table

    Returns The sub ‘Arrow::Table` that covers only rows of indices the values of `booleans` is true.

    Parameters:

    • booleans (::Array<Boolean>)

      The values indicating the target rows.

    Returns:

    • (Arrow::Table)

      The sub ‘Arrow::Table` that covers only rows of indices the values of `booleans` is true.

  • #slice(boolean_array) ⇒ Arrow::Table

    Returns The sub ‘Arrow::Table` that covers only rows of indices the values of `boolean_array` is true.

    Parameters:

    • boolean_array (::Array<Arrow::BooleanArray>)

      The values indicating the target rows.

    Returns:

    • (Arrow::Table)

      The sub ‘Arrow::Table` that covers only rows of indices the values of `boolean_array` is true.

  • #slice(range) ⇒ Arrow::Table

    Returns The sub ‘Arrow::Table` that covers only rows of the range of indices.

    Parameters:

    • range_included_end (Range)

      The range indicating the target rows.

    Returns:

    • (Arrow::Table)

      The sub ‘Arrow::Table` that covers only rows of the range of indices.

  • #slice {|slicer| ... } ⇒ Arrow::Table

    Returns The sub ‘Arrow::Table` that covers only rows matched by condition specified by slicer.

    Yields:

    • (slicer)

      Gives slicer that constructs condition to select records.

    Yield Parameters:

    • slicer (Arrow::Slicer)

      The slicer that helps us to build condition.

    Yield Returns:

    Returns:

    • (Arrow::Table)

      The sub ‘Arrow::Table` that covers only rows matched by condition specified by slicer.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/arrow/table.rb', line 270

def slice(*args)
  slicers = []
  if block_given?
    unless args.empty?
      raise ArgumentError, "must not specify both arguments and block"
    end
    block_slicer = yield(Slicer.new(self))
    case block_slicer
    when ::Array
      slicers.concat(block_slicer)
    else
      slicers << block_slicer
    end
  else
    expected_n_args = nil
    case args.size
    when 1
      if args[0].is_a?(Integer)
        index = args[0]
        index += n_rows if index < 0
        return nil if index < 0
        return nil if index >= n_rows
        return Record.new(self, index)
      else
        slicers << args[0]
      end
    when 2
      offset, length = args
      slicers << (offset...(offset + length))
    else
      expected_n_args = "1..2"
    end
    if expected_n_args
      message = "wrong number of arguments " +
        "(given #{args.size}, expected #{expected_n_args})"
      raise ArgumentError, message
    end
  end

  ranges = []
  slicers.each do |slicer|
    slicer = slicer.evaluate if slicer.respond_to?(:evaluate)
    case slicer
    when Integer
      slicer += n_rows if slicer < 0
      ranges << [slicer, n_rows - 1]
    when Range
      original_from = from = slicer.first
      to = slicer.last
      to -= 1 if slicer.exclude_end?
      from += n_rows if from < 0
      if from < 0 or from >= n_rows
        message =
          "offset is out of range (-#{n_rows + 1},#{n_rows}): " +
          "#{original_from}"
        raise ArgumentError, message
      end
      to += n_rows if to < 0
      ranges << [from, to]
    when ::Array
      boolean_array_to_slice_ranges(slicer, 0, ranges)
    when ChunkedArray
      offset = 0
      slicer.each_chunk do |array|
        boolean_array_to_slice_ranges(array, offset, ranges)
        offset += array.length
      end
    when BooleanArray
      boolean_array_to_slice_ranges(slicer, 0, ranges)
    else
      message = "slicer must be Integer, Range, (from, to), " +
        "Arrow::ChunkedArray of Arrow::BooleanArray, " +
        "Arrow::BooleanArray or Arrow::Slicer::Condition: #{slicer.inspect}"
      raise ArgumentError, message
    end
  end
  slice_by_ranges(ranges)
end

#slice_rawObject



221
# File 'lib/arrow/table.rb', line 221

alias_method :slice_raw, :slice

#to_s(options = {}) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/arrow/table.rb', line 481

def to_s(options={})
  format = options[:format]
  case format
  when :column
    return to_s_raw
  when :list
    formatter_class = TableListFormatter
  when :table, nil
    formatter_class = TableTableFormatter
  else
    message = ":format must be :column, :list, :table or nil"
    raise ArgumentError, "#{message}: <#{format.inspect}>"
  end
  formatter = formatter_class.new(self, options)
  formatter.format
end

#to_s_rawObject



480
# File 'lib/arrow/table.rb', line 480

alias_method :to_s_raw, :to_s

#window(size: nil) ⇒ Object

Experimental



464
465
466
# File 'lib/arrow/table.rb', line 464

def window(size: nil)
  RollingWindow.new(self, size)
end