Read streaming batches from a Parquet file. Parameters ---------- batch_size : int, default 64K Maximum number of records to yield per batch. Batches may be smaller if there aren't enough rows in the file. row_groups : list
(self, batch_size=65536, row_groups=None, columns=None,
use_threads=True, use_pandas_metadata=False)
| 529 | use_threads=use_threads) |
| 530 | |
| 531 | def iter_batches(self, batch_size=65536, row_groups=None, columns=None, |
| 532 | use_threads=True, use_pandas_metadata=False): |
| 533 | """ |
| 534 | Read streaming batches from a Parquet file. |
| 535 | |
| 536 | Parameters |
| 537 | ---------- |
| 538 | batch_size : int, default 64K |
| 539 | Maximum number of records to yield per batch. Batches may be |
| 540 | smaller if there aren't enough rows in the file. |
| 541 | row_groups : list |
| 542 | Only these row groups will be read from the file. |
| 543 | columns : list |
| 544 | If not None, only these columns will be read from the file. A |
| 545 | column name may be a prefix of a nested field, e.g. 'a' will select |
| 546 | 'a.b', 'a.c', and 'a.d.e'. |
| 547 | use_threads : boolean, default True |
| 548 | Perform multi-threaded column reads. |
| 549 | use_pandas_metadata : boolean, default False |
| 550 | If True and file has custom pandas schema metadata, ensure that |
| 551 | index columns are also loaded. |
| 552 | |
| 553 | Yields |
| 554 | ------ |
| 555 | pyarrow.RecordBatch |
| 556 | Contents of each batch as a record batch |
| 557 | |
| 558 | Examples |
| 559 | -------- |
| 560 | Generate an example Parquet file: |
| 561 | |
| 562 | >>> import pyarrow as pa |
| 563 | >>> table = pa.table({'n_legs': [2, 2, 4, 4, 5, 100], |
| 564 | ... 'animal': ["Flamingo", "Parrot", "Dog", "Horse", |
| 565 | ... "Brittle stars", "Centipede"]}) |
| 566 | >>> import pyarrow.parquet as pq |
| 567 | >>> pq.write_table(table, 'example.parquet') |
| 568 | >>> parquet_file = pq.ParquetFile('example.parquet') |
| 569 | >>> for i in parquet_file.iter_batches(): |
| 570 | ... print("RecordBatch") |
| 571 | ... print(i.to_pandas()) |
| 572 | ... |
| 573 | RecordBatch |
| 574 | n_legs animal |
| 575 | 0 2 Flamingo |
| 576 | 1 2 Parrot |
| 577 | 2 4 Dog |
| 578 | 3 4 Horse |
| 579 | 4 5 Brittle stars |
| 580 | 5 100 Centipede |
| 581 | """ |
| 582 | if batch_size <= 0: |
| 583 | raise ValueError("batch_size must be greater than zero") |
| 584 | |
| 585 | if row_groups is None: |
| 586 | row_groups = range(0, self.metadata.num_row_groups) |
| 587 | column_indices = self._get_column_indices( |
| 588 | columns, use_pandas_metadata=use_pandas_metadata) |