Sub topic 9
Choosing file formats and layouts
This chapter assumes you know basic Polars expressions and can tell CSV from Parquet. It also assumes you have seen scan_csv, scan_parquet, and query plans from earlier chapters. Here you will learn how the format and layout of your input files change what the streaming engine can skip, and how much memory it needs.
Why the format matters
The streaming engine reads data in chunks, so the file must let it read part of the data without reading all of it. Formats differ a lot here.
| Property | CSV | Parquet |
|---|---|---|
| Storage | Text, row by row | Binary, column by column |
| Column selection | Must read every line to find a column | Reads only the needed column chunks |
| Filter pushdown | Rows are parsed, then dropped | Row groups can be skipped using min/max statistics |
| Types | Guessed from the text | Stored in the file |
| Size on disk | Large | Small, because of compression |
With CSV, Polars has to parse the text of every row, even if the query needs one column. With Parquet, the projection pushdown you saw earlier means unused columns are never read. Predicate pushdown can also skip whole row groups. A row group is a block of rows inside a Parquet file, and the file footer stores statistics such as the minimum and maximum of each column for every group.
Convert once, query many times
If you query a large CSV repeatedly, convert it to Parquet once. The conversion itself can run without loading the file, using a sink.
import polars as pl
pl.scan_csv("events.csv").sink_parquet("events.parquet")After this, later queries use scan_parquet("events.parquet") and benefit from pushdown. When you must stay with CSV, pass explicit types with the schema argument of scan_csv. This avoids wrong guesses from type inference and keeps memory use predictable.
Many files instead of one
A single huge file is harder to manage than a set of moderate files. A glob pattern lets one scan cover all of them.
import polars as pl
lf = (
pl.scan_parquet("events/*.parquet")
.filter(pl.col("status") == "ok")
.select("user_id", "amount")
)
print(lf.explain())Splitting data into files has three benefits:
- The engine can read different files in parallel.
- You can add new data by writing a new file, without rewriting old data.
- A query can ignore files it does not need, if the layout helps it do so.
Avoid the opposite extreme. Thousands of tiny files add overhead for opening files and reading footers. A common target is files of roughly 100 MB to 1 GB each, though the right size depends on your storage and machine.
Partitioned layouts
Partitioning means putting files in folders named after a column value, for example events/date=2026-12-01/part-0.parquet. Polars can read this style when you ask for it.
import polars as pl
lf = pl.scan_parquet("events/", hive_partitioning=True)
result = lf.filter(pl.col("date") == "2026-12-01").collect(engine="streaming")When the filter uses the partition column, Polars can skip every folder that does not match, so those files are never opened. Choose partition columns that you often filter on and that have a moderate number of distinct values. A column such as user_id with millions of values would create millions of folders.
Sorting data inside files also helps. If rows are ordered by a column you filter on, each row group covers a narrow range of values. The min/max statistics then rule out more groups.
Practical checklist
- Prefer Parquet over CSV for data you query more than once.
- Use a glob or a directory of moderate files instead of one giant file.
- Partition by a column you filter on often, with a limited number of values.
- Sort within files by your most common filter column.
- Confirm with
explain()that filters and column selection reach the scan.
Exercise
Your team has a 200 GB CSV file that analysts query daily, usually for one month of data and three columns. Describe how you would change the storage, and why it helps the streaming engine.
Show answer
Convert the CSV to Parquet once with scancsv(...).sinkparquet(...), or better, write partitioned Parquet files split by month. Queries for one month then skip the other partitions entirely. Because Parquet is columnar, reading three columns avoids reading the rest. Row group statistics let the engine skip further data. Check with explain() that the filter and column selection appear at the scan.