Sub topic 3
Columnar formats: why Parquet and Arrow matter
This chapter assumes you can load a dataset with pandas or SQL and know what rows, columns, and data types are. It explains two formats that the later chapters rely on: Parquet for files on disk, and Arrow for data in memory.
Row layout and column layout
A CSV file stores data row by row. To sum one column, a reader must still scan every row and parse every field in it. A columnar format stores the values of each column together. A query that needs two columns out of twenty can skip the other eighteen.
Columnar storage helps analytics for three reasons:
- Column pruning. The engine reads only the columns the query uses.
- Compression. Values in one column share a type and are often similar, so they compress well. A
regioncolumn with a few repeated values shrinks a lot. - Fast computation. Operating on a contiguous array of one type works well with modern CPUs.
Parquet: columnar storage on disk
Parquet is an open file format for tables. A file is split into row groups, and each row group stores one chunk per column. The file also keeps statistics, such as the minimum and maximum of each column chunk, and the schema with its data types.
These features give you two optimizations:
- Column pruning reads only the requested column chunks.
- Predicate pushdown uses the min/max statistics to skip row groups that cannot match a filter. If a chunk's maximum
revenueis 50, a filterrevenue > 100skips it without reading it.
Parquet also keeps data types, unlike CSV, where every value is text and each reader must guess the type. Polars, DuckDB, and Spark can all push column selection and filters down into a Parquet scan. pandas can select columns with columns=[...], as in the first example of the same-task chapter.
Arrow: columnar data in memory
Parquet is a storage format. Apache Arrow is a specification for how columnar data is laid out in memory, with libraries that implement it. Polars uses Arrow-style memory layout internally. DuckDB, pandas, and Spark can exchange data with Arrow-based interfaces.
When two tools agree on the same memory layout, they can often share a buffer instead of converting every value. This is why the conversion shown below is cheap compared with writing a file and reading it back.
import duckdb
import pandas as pd
pdf = pd.DataFrame({"region": ["eu", "us", "eu"], "revenue": [10, 20, 5]})
plf = duckdb.sql(
"SELECT region, sum(revenue) AS revenue FROM pdf GROUP BY region"
).pl()
back_to_pandas = plf.to_pandas()
print(back_to_pandas)DuckDB finds pdf by its variable name. The .pl() and .to_pandas() conversions need pyarrow installed. Even with Arrow, some conversions copy data, for example when pandas needs its own NumPy-based layout, so large conversions still cost memory.
Practical rules
| Situation | Advice |
|---|---|
| Data is stored as CSV and queried often | Convert it once to Parquet |
| Many small queries on a few columns | Store as Parquet and select columns at read time |
| Passing data between tools in one process | Use Arrow-backed interfaces |
| Data must be edited row by row | Parquet is a poor fit, because files are written whole |
Exercise
A 20-column Parquet file holds 100 million rows. Your query filters on revenue and sums it by region. Name two things the engine can skip, and the Parquet feature behind each.
Show answer
It can skip the 18 unused columns, which is column pruning. It can also skip row groups whose min/max statistics for revenue show no row can pass the filter, which is predicate pushdown.