Class: Arrow::Table
- Inherits:
-
Object
- Object
- Arrow::Table
- Defined in:
- lib/arrow/table.rb
Class Method Summary collapse
Instance Method Summary collapse
- #each_record_batch ⇒ Object
-
#group(*keys) ⇒ Object
Experimental.
-
#initialize(*args) ⇒ Table
constructor
Creates a new Table.
- #inspect ⇒ Object
- #inspect_raw ⇒ Object
-
#join(right, keys = nil, type: :inner, left_suffix: "", right_suffix: "", left_outputs: nil, right_outputs: nil) ⇒ Arrow::Table
Join another Table by matching with keys.
-
#merge(other) ⇒ Arrow::Table
TODO.
- #method_missing(name, *args, &block) ⇒ Object
- #pack ⇒ Object
- #remove_column(name_or_index) ⇒ Object
- #remove_column_raw ⇒ Object
- #respond_to_missing?(name, include_private) ⇒ Boolean
- #save(output, options = {}) ⇒ Object
- #slice(*args) ⇒ Object
- #slice_raw ⇒ Object
- #to_s(options = {}) ⇒ Object
- #to_s_raw ⇒ Object
-
#window(size: nil) ⇒ Object
Experimental.
Methods included from RecordContainable
Methods included from InputReferable
Methods included from GenericTakeable
Methods included from GenericFilterable
Methods included from ColumnContainable
#[], #column_names, #columns, #each_column, #find_column, #select_columns
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.
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/arrow/table.rb', line 163 def initialize(*args) n_args = args.size case n_args when 1 raw_table_converter = RawTableConverter.new(args[0]) schema = raw_table_converter.schema values = raw_table_converter.values 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 = "wrong number of arguments (given #{n_args}, expected 1..2)" raise ArgumentError, 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
622 623 624 625 626 627 628 |
# File 'lib/arrow/table.rb', line 622 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
29 30 31 |
# File 'lib/arrow/table.rb', line 29 def load(path, ={}) TableLoader.load(path, ) end |
Instance Method Details
#each_record_batch ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/arrow/table.rb', line 187 def each_record_batch return to_enum(__method__) unless block_given? reader = TableBatchReader.new(self) while record_batch = reader.read_next share_input(record_batch) yield(record_batch) end end |
#group(*keys) ⇒ Object
Experimental
436 437 438 |
# File 'lib/arrow/table.rb', line 436 def group(*keys) Group.new(self, keys) end |
#inspect ⇒ Object
613 614 615 |
# File 'lib/arrow/table.rb', line 613 def inspect "#{super}\n#{to_s}" end |
#inspect_raw ⇒ Object
612 |
# File 'lib/arrow/table.rb', line 612 alias_method :inspect_raw, :inspect |
#join(right, type: :inner, left_outputs: nil, right_outputs: nil) ⇒ Arrow::Table #join(right, key, type: :inner, left_outputs: nil, right_outputs: nil) ⇒ Arrow::Table #join(right, keys, type: :inner, left_suffix: "", right_suffix: "") ⇒ Arrow::Table #join(right, keys, type: :inner, left_outputs: nil, right_outputs: nil) ⇒ Arrow::Table
Join another Table by matching with keys.
Join columns with ‘right` on join key columns.
Join right by keys.
Column name can be renamed by appending `left_suffix` or `right_suffix`.
@macro join_common_before
@param keys [::Array<String, Symbol>] Join keys.
@macro join_common_after
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'lib/arrow/table.rb', line 527 def join(right, keys=nil, type: :inner, left_suffix: "", right_suffix: "", left_outputs: nil, right_outputs: nil) is_natural_join = keys.nil? keys ||= (column_names & right.column_names) type = JoinType.try_convert(type) || type plan = ExecutePlan.new left_node = plan.build_source_node(self) right_node = plan.build_source_node(right) if keys.is_a?(Hash) left_keys = keys[:left] right_keys = keys[:right] else left_keys = keys right_keys = keys end left_keys = Array(left_keys) right_keys = Array(right_keys) = HashJoinNodeOptions.new(type, left_keys, right_keys) use_manual_outputs = false unless left_outputs.nil? .left_outputs = left_outputs use_manual_outputs = true end unless right_outputs.nil? .right_outputs = right_outputs use_manual_outputs = true end hash_join_node = plan.build_hash_join_node(left_node, right_node, ) type_nick = type.nick is_filter_join = (type_nick.end_with?("-semi") or type_nick.end_with?("-anti")) if use_manual_outputs or is_filter_join process_node = hash_join_node elsif is_natural_join process_node = join_merge_keys(plan, hash_join_node, right, keys) elsif keys.is_a?(String) or keys.is_a?(Symbol) process_node = join_merge_keys(plan, hash_join_node, right, [keys.to_s]) elsif !keys.is_a?(Hash) and (left_suffix != "" or right_suffix != "") process_node = join_rename_keys(plan, hash_join_node, right, keys, left_suffix, right_suffix) else process_node = hash_join_node end = SinkNodeOptions.new plan.build_sink_node(process_node, ) plan.validate plan.start plan.wait reader = .get_reader(process_node.output_schema) table = reader.read_all share_input(table) table end |
#merge(other) ⇒ Arrow::Table
TODO
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 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/arrow/table.rb', line 360 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 = "merge target must be Hash or Arrow::Table: " + "<#{other.inspect}>: #{inspect}" raise ArgumentError, 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 table = self.class.new(new_fields, new_arrays) share_input(table) table end |
#pack ⇒ Object
450 451 452 453 454 455 456 457 |
# File 'lib/arrow/table.rb', line 450 def pack packed_arrays = columns.collect do |column| column.data.pack end table = self.class.new(schema, packed_arrays) share_input(table) table end |
#remove_column(name_or_index) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/arrow/table.rb', line 412 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? = "unknown column: #{name_or_index.inspect}: #{inspect}" raise KeyError.new() end else index = name_or_index index += n_columns if index < 0 if index < 0 or index >= n_columns = "out of index (0..#{n_columns - 1}): " + "#{name_or_index.inspect}: #{inspect}" raise IndexError.new() end end table = remove_column_raw(index) share_input(table) table end |
#remove_column_raw ⇒ Object
411 |
# File 'lib/arrow/table.rb', line 411 alias_method :remove_column_raw, :remove_column |
#respond_to_missing?(name, include_private) ⇒ Boolean
617 618 619 620 |
# File 'lib/arrow/table.rb', line 617 def respond_to_missing?(name, include_private) return true if find_column(name) super end |
#save(output, options = {}) ⇒ Object
445 446 447 448 |
# File 'lib/arrow/table.rb', line 445 def save(output, ={}) saver = TableSaver.new(self, output, ) saver.save end |
#slice(offset, length) ⇒ Arrow::Table #slice(index) ⇒ Arrow::Record #slice(booleans) ⇒ Arrow::Table #slice(boolean_array) ⇒ Arrow::Table #slice(range) ⇒ Arrow::Table #slice(conditions) ⇒ Arrow::Table #slice {|slicer| ... } ⇒ Arrow::Table
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 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 348 349 350 351 352 353 354 355 |
# File 'lib/arrow/table.rb', line 255 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 case args[0] when 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) when Hash condition_pairs = args[0] slicer = Slicer.new(self) conditions = [] condition_pairs.each do |key, value| case value when Range # TODO: Optimize "begin <= key <= end" case by missing "between" kernel # https://issues.apache.org/jira/browse/ARROW-9843 unless value.begin.nil? conditions << (slicer[key] >= value.begin) end unless value.end.nil? if value.exclude_end? conditions << (slicer[key] < value.end) else conditions << (slicer[key] <= value.end) end end else conditions << (slicer[key] == value) end end slicers << conditions.inject(:&) 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 = "wrong number of arguments " + "(given #{args.size}, expected #{expected_n_args})" raise ArgumentError, end end sliced_tables = [] slicers.each do |slicer| slicer = slicer.evaluate if slicer.respond_to?(:evaluate) case slicer when Integer slicer += n_rows if slicer < 0 sliced_tables << slice_by_range(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 = "offset is out of range (-#{n_rows + 1},#{n_rows}): " + "#{original_from}" raise ArgumentError, end to += n_rows if to < 0 sliced_tables << slice_by_range(from, to) when ::Array, BooleanArray, ChunkedArray sliced_tables << filter(slicer) else = "slicer must be Integer, Range, (from, to), " + "Arrow::ChunkedArray of Arrow::BooleanArray, " + "Arrow::BooleanArray or Arrow::Slicer::Condition: #{slicer.inspect}" raise ArgumentError, end end if sliced_tables.size > 1 sliced_table = sliced_tables[0].concatenate(sliced_tables[1..-1]) else sliced_table = sliced_tables[0] end share_input(sliced_table) sliced_table end |
#slice_raw ⇒ Object
200 |
# File 'lib/arrow/table.rb', line 200 alias_method :slice_raw, :slice |
#to_s(options = {}) ⇒ Object
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'lib/arrow/table.rb', line 595 def to_s(={}) format = [:format] case format when :column return to_s_raw when :list formatter_class = TableListFormatter when :table, nil formatter_class = TableTableFormatter else = ":format must be :column, :list, :table or nil" raise ArgumentError, "#{}: <#{format.inspect}>" end formatter = formatter_class.new(self, ) formatter.format end |
#to_s_raw ⇒ Object
594 |
# File 'lib/arrow/table.rb', line 594 alias_method :to_s_raw, :to_s |
#window(size: nil) ⇒ Object
Experimental
441 442 443 |
# File 'lib/arrow/table.rb', line 441 def window(size: nil) RollingWindow.new(self, size) end |