Sub topic 5
Arrays, record batches and tables
This chapter assumes you know basic Python, have pyarrow installed, and have seen the ideas of buffers, validity bitmaps, data types and schemas from the earlier chapters. Here we look at the three containers that hold Arrow data: arrays, record batches and tables.
The three containers
An array is one column of values of a single type. It is made of the buffers you saw earlier: a validity bitmap, and a data buffer (plus offsets for variable-width types). A record batch is a set of equal-length arrays that share a schema. A table is a set of columns that follow a schema, where each column can be split into several pieces.
| Container | Holds | Length of columns | Typical use |
|---|---|---|---|
| Array | One column, one contiguous piece | n/a | Computing on a single column |
| Record batch | Equal-length arrays plus a schema | All identical | Streaming and IPC messages |
| Table | Chunked columns plus a schema | Same total length, chunks may differ | Holding a whole dataset in memory |
Building an array
An array has a type. If you do not give one, pyarrow infers it from the values.
import pyarrow as pa
arr = pa.array([1, None, 3], type=pa.int32())
print(arr.null_count)
print(len(arr.buffers()))The array reports one null. It has two buffers: the validity bitmap and the data buffer.
Building a table from a schema
A schema fixes the name and type of each column. A table built with it must match.
import pyarrow as pa
schema = pa.schema([
("id", pa.int64()),
("name", pa.string()),
])
table = pa.table({"id": [1, 2, 3], "name": ["a", None, "c"]}, schema=schema)
print(table.schema)
print(table.num_rows)The values are checked against the schema. A string in the id column raises an error instead of being stored.
Chunks and record batches
Each column of a table is a ChunkedArray: a list of arrays that act as one long column. Chunking lets you append data by adding a chunk, without copying the existing ones. A record batch is the single-chunk building block. Tables are often made by combining batches.
import pyarrow as pa
b1 = pa.record_batch({"id": [1, 2], "name": ["a", "b"]})
b2 = pa.record_batch({"id": [3], "name": ["c"]})
table = pa.Table.from_batches([b1, b2])
print(table.num_rows)
print(table.column("id").num_chunks)
print(table.to_batches()[0].num_rows)This prints 3, 2 and 2. The table has three rows, and its id column has two chunks.
Common errors
- Assuming a table is one contiguous block. A column may have many chunks, so code that needs one array must call
combine_chunks(), which copies the data. - Mismatched batches.
Table.from_batchesfails if batches have different schemas. - Mixed types in one list.
pa.array([1, "a"])raisesArrowInvalid, because an array holds one type. - Expecting mutation. Arrays and tables are immutable. Operations such as
append_columnreturn a new object, and they share the existing buffers where possible.
Exercise
A table has a column with 3 chunks. You need a single Array for that column. Which call gives it to you, and what is the cost?
Show answer
Call table.column("name").combine_chunks(). It concatenates the chunks into new buffers, so it copies the data and uses extra memory.