Sub topic 12
Processing larger-than-memory data in batches
This chapter assumes you can open a partitioned dataset with ds.dataset and build filters with ds.field. It also assumes you know that a Parquet file is split into row groups. Here you will process a dataset that does not fit in memory by reading it one record batch at a time.
Why `to_table` can fail
dataset.to_table() loads every selected row into one in-memory Arrow table. If the selection is larger than your RAM, the process runs out of memory. A scanner avoids this by yielding small pieces of data instead of a single large table.
A record batch is a chunk of rows that has the same schema as the table. A scanner reads files and row groups lazily and hands you one batch at a time. Once you drop a batch, its memory can be freed.
Scanning in batches
The scanner method takes the same filter and columns arguments as to_table. Calling to_batches() on it returns an iterator.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
scanner = dataset.scanner(
filter=ds.field("year") >= 2023,
columns=["country", "amount"],
)
total = 0.0
for batch in scanner.to_batches():
total += sum(v for v in batch.column("amount").to_pylist() if v is not None)
print(total)The loop keeps only a running total. Memory use depends on the batch size, not on the dataset size.
What limits memory
Three things keep memory low in the example above.
- The filter on
yearis a partition filter, so directories that do not match are never opened. - The
columnslist means onlycountryandamountare read from each file. - Batches are consumed and discarded one by one.
The scanner also reads ahead to keep the CPU busy. If memory is still tight, pass a smaller batch_size to scanner. The default is 131,072 rows. Wide rows need a smaller value. You can also set batch_readahead and fragment_readahead to reduce how much is buffered.
Common errors
- Calling
to_tableinside the loop or afterward. This undoes the benefit. Keep the work inside the loop. - Collecting all batches into a list.
list(scanner.to_batches())holds everything in memory at once. - Converting each batch to Python objects.
to_pylist()is slow and creates many objects. Usepyarrow.computefor the arithmetic on each batch instead.
Here is the same sum with pyarrow.compute:
import pyarrow.compute as pc
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
scanner = dataset.scanner(
filter=ds.field("year") >= 2023,
columns=["amount"],
)
total = 0.0
for batch in scanner.to_batches():
batch_sum = pc.sum(batch.column("amount")).as_py()
if batch_sum is not None:
total += batch_sum
print(total)pc.sum skips nulls and returns a null scalar when the batch is empty or all null, so the check keeps the loop safe.
Exercise
A dataset is 200 GB and your machine has 16 GB of RAM. You want the total of amount for year == 2024. Which two arguments to dataset.scanner keep memory use low, and how do you consume the result?
Show answer
Use filter=ds.field("year") == 2024 so only matching partition directories are read, and columns=["amount"] so only that column is read. Then iterate over scanner.to_batches() and add each batch's sum to a running total instead of calling to_table.