Sub topic 11
Arrow and Parquet: in-memory versus on-disk
This chapter assumes you know what record batches and tables are, what a schema is, and how the IPC file format works. It also assumes you can run pyarrow code.
Two formats for two jobs
Arrow and Parquet both store columns, and they are often used together. They solve different problems.
- Arrow is a layout for data in memory. Its buffers are uncompressed and arranged so that code can read values directly, with no decoding step.
- Parquet is a layout for data in files. It encodes and compresses each column to save disk space and I/O, so it must be decoded before use.
Reading a Parquet file with pyarrow decodes the file into Arrow arrays. Writing a Parquet file encodes Arrow arrays into the file format. Parquet is the storage format and Arrow is the working format.
How Parquet is organized
A Parquet file is split into row groups, and each row group stores one column chunk per column. A footer at the end of the file holds the schema and per-chunk statistics such as minimum and maximum values.
This structure allows two optimizations:
- Column projection: the reader fetches only the column chunks you ask for.
- Row group skipping: the reader compares your filter with the min/max statistics and skips row groups that cannot match.
Writing and reading Parquet
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": [1, 2, 3, 4],
"name": ["a", None, "c", "d"],
"score": [0.5, 1.5, 2.5, 3.5],
})
pq.write_table(table, "example.parquet", compression="zstd", row_group_size=2)
subset = pq.read_table(
"example.parquet",
columns=["id", "name"],
filters=[("id", ">", 2)],
)
print(subset.schema)
print(subset.num_rows)
meta = pq.ParquetFile("example.parquet").metadata
print(meta.num_row_groups, meta.num_columns)The file has two row groups of two rows each. The filter id > 2 can skip the first group, whose maximum id is 2. The result has two rows and no score column. Row group skipping is a saving, not a guarantee, so a filter on unsorted data may skip nothing.
What a round trip preserves
Parquet has its own type system, and pyarrow stores the Arrow schema in the file's metadata to restore types on read. Check the schema after a round trip when your types are unusual, such as dictionary-encoded columns or non-default timestamp units. Nulls survive the trip, because Parquet records them separately from values.
Choosing between Arrow IPC and Parquet
| Question | Arrow IPC file | Parquet |
|---|---|---|
| Size on disk | Larger, mostly uncompressed | Smaller, encoded and compressed |
| Read cost | Very low, and memory mapping can avoid copies | Higher, needs decoding |
| Partial reads | Whole batches | Column chunks and row groups |
| Typical use | Short-lived exchange, caches, scratch data | Long-term storage, shared data lakes |
Use IPC when the data is read soon and often on the same machine, and read speed matters most. Use Parquet when the data lives a long time or is stored at scale.
Common errors
- Reading a whole Parquet file when you need three columns. Pass
columns=. - Writing many tiny row groups. Each group adds metadata and weakens compression, so use sizes in the thousands to millions of rows.
- Expecting a memory-mapped, zero-copy read from Parquet. Decoding always creates new buffers.
- Assuming the file size equals the memory size. A compressed Parquet file can expand a lot when decoded.
Exercise
You store a 20 GB Parquet dataset and often need only two of its 40 columns, filtered by a date column. Which two read options reduce the work, and why does a memory map not help here?
Show answer
Pass columns= to read only the two column chunks, and pass a filter so the reader can skip row groups whose date statistics cannot match. A memory map avoids copies only for Arrow IPC files, where the file bytes already match the in-memory layout. Parquet bytes are encoded and compressed, so they must be decoded into new Arrow buffers.