Sub topic 5
Scanning CSV and Parquet instead of reading them
This chapter assumes you can build a LazyFrame and call collect(). It also assumes you know the expression API (pl.col, filter, select). Here you will see how file scans differ from file reads, and why that matters to the optimiser.
Read versus scan
pl.read_csv() and pl.read_parquet() are eager. They load the whole file into a DataFrame before your next line runs. pl.scan_csv() and pl.scan_parquet() are lazy. They return a LazyFrame whose plan starts with a scan node. That node records the path and the file options, and it does not hold any rows.
import polars as pl
pl.DataFrame({
"region": ["EU", "US", "EU", "APAC"],
"product": ["a", "b", "a", "c"],
"amount": [10.0, 20.0, 5.0, 7.5],
}).write_csv("sales.csv")
lf = pl.scan_csv("sales.csv")
print(type(lf))
query = lf.filter(pl.col("region") == "EU").select("product", "amount")
print(query.collect())Calling scan_csv only prepares the plan. Polars reads the file during collect(), after the optimiser has seen the whole query.
Why scanning helps
The optimiser can pass information down into the scan node. Two rewrites matter here:
- Projection pushdown. Only the columns the query uses are read.
- Predicate pushdown. Filters are applied inside the scan, so rows that fail them are dropped as early as possible.
A read function cannot do either, because it has to finish before it sees your query.
Parquet and CSV differ
Both formats benefit, but not equally.
| Feature | CSV | Parquet |
|---|---|---|
| Storage | Row-oriented text | Column-oriented binary |
| Skip unused columns | Fields are still found in each line, but unused ones are not kept | Unused column chunks are not read |
| Skip rows by filter | Rows must be parsed to be tested | Row group statistics can let whole blocks be skipped |
| Schema | Inferred from a sample of rows | Stored in the file |
Parquet gains the most, because it was designed for reading selected columns and blocks.
Inspecting the scan node
Use explain() to see what reached the scan:
import polars as pl
pl.DataFrame({
"user_id": [1, 2, 3],
"ts": [10, 20, 30],
"country": ["NO", "SE", "NO"],
"payload": ["x", "y", "z"],
}).write_parquet("events.parquet")
lf = (
pl.scan_parquet("events.parquet")
.select("user_id", "ts", "country")
.filter(pl.col("country") == "NO")
)
print(lf.explain())In the optimised plan, the scan line shows that only some of the columns are projected. It also shows the country condition as a selection. The payload column is never read.
Scanning many files
Both scan functions accept a glob pattern such as pl.scan_parquet("data/*.parquet"). The files are treated as one table, and the same pushdowns apply.
Exercise
You run pl.scan_parquet("big.parquet").select("a").filter(pl.col("b") > 0).collect(). Which columns does the scan need to read, and why?
Show answer
It needs both a and b. Column a is in the output, and column b is used by the filter. Projection pushdown keeps both in the scan and skips every other column.