The lazy engine in Python
A LazyFrame builds a plan; collect runs it after the optimizer rewrites it.
import polars as pl
frame = pl.DataFrame({
"lang": ["python", "rust", "sql", "css"],
"tpm": [104, 98, 79, 70],
"note": ["a", "b", "c", "d"],
})
plan = (frame.lazy()
.filter(pl.col("tpm") > 80)
.select("lang", "tpm")
.sort("tpm", descending=True))
print(plan.explain())
print(plan.collect())
How it works
explainprints the plan, showing pushed-down filters.- Projection pushdown reads only the columns you use.
- The eager and lazy answers are identical by construction.
Keywords and builtins used here
asprint
The run, in numbers
- Lines
- 15
- Characters to type
- 293
- Tokens
- 132
- Three-star pace
- 115 tpm
At the three-star pace of 115 tokens a minute, this run takes about 69 seconds.
Step 2 of 4 in Lazy & SQL, step 26 of 32 in Polars.