Sub topic 5
Polars: a fast DataFrame engine with a lazy API
This chapter assumes you know basic pandas, can read a group-by, and understand the eager versus lazy distinction from the earlier chapter on execution models. It covers what Polars is, how its lazy API works, and where it fits among the four tools.
What Polars is
Polars is a DataFrame library written in Rust, with a Python API. It runs on a single machine and uses all available CPU cores by default. It stores data in a columnar layout based on the Arrow format, so column operations work on contiguous memory.
Polars offers two modes:
- Eager:
pl.read_parquet(...)loads data now, and each operation runs immediately, like pandas. - Lazy:
pl.scan_parquet(...)returns aLazyFrame. Operations build a plan, and nothing runs until you callcollect().
The lazy workflow
The lazy version of a filter-and-aggregate looks like this:
import polars as pl
result = (
pl.scan_parquet("sales.parquet")
.group_by("region")
.agg(pl.col("revenue").sum())
.collect()
)
print(result)scan_parquet reads only file metadata at first. Because the whole query is known before it runs, the optimizer can rewrite it. Two common rewrites are projection pushdown (read only the columns the query uses) and predicate pushdown (apply filters while reading, so rows that fail are discarded early).
You can inspect the plan before running anything:
import polars as pl
lf = (
pl.scan_parquet("sales.parquet")
.filter(pl.col("revenue") > 0)
.select("region", "revenue")
)
print(lf.explain())In the output, look for the selected columns and the filter appearing inside the scan step. That shows the optimizer moved the work down to the file read.
Expressions
Polars code is built from expressions such as pl.col("revenue").sum(). An expression describes a computation on a column and is not tied to one DataFrame. The engine can run independent expressions in parallel inside a single select, with_columns, or agg call. This is why Polars code avoids row-by-row Python functions.
Strengths
- Multi-core execution without extra setup.
- Lazy optimization on a single machine.
- Good fit for Parquet pipelines and repeatable transformations.
- The streaming engine can process some queries on data larger than memory.
Limits
- Single node. Data far beyond one machine's disk and memory needs another approach.
- The API differs from pandas. There is no index, and many pandas idioms have different names, so migration means rewriting code.
- Not every operation works in streaming mode, so test larger-than-memory queries on your own data.
- The ecosystem is smaller than pandas, though conversion with
to_pandas()covers many gaps. - Python functions applied per row (
map_elements) run slowly and lose the parallel advantage.
Common errors
- Calling
read_parqueton a large file when a scan would do. This loads everything and skips optimization. Usescan_parquetandcollect()at the end. - Calling
collect()too early. Collecting in the middle of a pipeline forces execution and discards later optimizations. - Writing pandas-style row loops or
applycalls. Express the logic withpl.col(...)expressions. - Expecting an index. Polars has none. Use columns and joins.
Exercise
You run pl.read_parquet("big.parquet").filter(pl.col("revenue") > 0).select("region", "revenue") on a file with 50 columns. How would you change it so Polars reads fewer columns and applies the filter during the scan, and what call finally runs it?
Show answer
Replace read_parquet with scan_parquet, keeping the same filter and select, so the result is a LazyFrame. The optimizer then applies projection and predicate pushdown. Calling collect() runs the query.