Ticks do not arrive as one finished table. They arrive in pieces, a few thousand at a time, and the pipeline that handles them cannot wait for the day to end. Arrow's answer is the record batch: a small, complete slab of rows that can be computed on, sent or saved the moment it exists, then replaced by the next one.
The idea in plain words
Where we are. Topic 1 showed what a column is. This topic groups columns into batches and tables, which are the objects every Arrow tool passes around. The next topic computes on them, and the later IPC and Flight topics are about sending exactly these batches.
A set of equal-length arrays with a schema: one contiguous slab of rows. It is the unit Arrow computes on, streams and writes to files. A table is a list of chunks, usually one per batch.
Cut a table into batches and put it back
The bars table is not one block. It was built from five stocks, so it has five chunks, and to_batches never makes a batch that straddles two of them. Asking for batches of at most 10,000 rows therefore gives five batches of about 6,700, not four of 10,000. table_from_batches goes the other way and wraps the same memory in a table, one chunk per batch, without copying a value.
# Cut the bars into batches of at most 10,000 rows, then wrap the batches in a table againsizes = batch_sizes(table, 10000)print(len(sizes), "batches:", sizes)batches = table.to_batches(max_chunksize=10000)again = table_from_batches(batches)print("rows:", again.num_rows, "| chunks per column:", again["close"].num_chunks, "| equal:", again.equals(table))
5 batches: [6699, 6698, 6698, 6699, 6699] rows: 33493 | chunks per column: 5 | equal: True
Work one batch at a time
batches_total adds up a column across batches without ever joining them: it computes a partial sum per batch and carries the running total. Only one batch needs to be in memory at a time, so the same code works on a day of data or ten years, as long as the batches arrive from a stream. A null-only batch adds nothing, which is why pc.sum returns None for it and the code treats that as zero.
# Add up the volume batch by batch and compare with summing the whole table at onceimport pyarrow.compute as pcprint("by batches :", batches_total(batches, "volume"))print("whole table:", pc.sum(table["volume"]).as_py())
by batches : 3398140608402 whole table: 3398140608402
Make a table fit a schema
Two systems agree on a schema or they do not talk. conform takes the columns a consumer expects, in its order, with its types, and drops the rest. cast converts safely: it raises an error rather than silently overflowing, which is why a price can become float32 but a share volume of three billion cannot become int32.
# A consumer wants volume, close and symbol, in that order, with narrower types for close and symboltarget = pa.schema([("volume", pa.int64()), ("close", pa.float32()), ("symbol", pa.large_string())])fitted = conform(table, target)print(fitted.schema)try: conform(table, pa.schema([("volume", pa.int32())]))except Exception as error: print(type(error).__name__ + ": volume does not fit in int32")
volume: int64 close: float symbol: large_string ArrowInvalid: volume does not fit in int32
Too small and every batch pays fixed costs for its header and function calls; too large and a batch no longer fits in cache or arrives late. Thousands to a few hundred thousand rows is the usual range. Pick it from the slowest stage of the pipeline and measure, rather than copying a number.
What the tasks ask
- 1`batch_sizes` and `table_from_batches`
table.to_batches(max_chunksize=...)and take eachnum_rows;pa.Table.from_batches(batches). - 2`batches_total`For each batch, add
pc.sum(batch[column]).as_py() or 0; nofrom_batches, noto_pylist. - 3`conform`
table.select(schema.names).cast(schema).
table.to_batches(max_chunksize=10000) on a table with five chunks of about 6,700 rows gives how many batches?Lab · what's graded
4 tasks · 100 pts- Cut into batches:
batch_sizes25 pts - Put them back together:
table_from_batches25 pts - Sum without one big table:
batches_total25 pts - Match a target schema:
conform25 pts