typestar

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

  1. explain prints the plan, showing pushed-down filters.
  2. Projection pushdown reads only the columns you use.
  3. The eager and lazy answers are identical by construction.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 4 in Lazy & SQL, step 26 of 32 in Polars.

← Previous Next →