Optimizing Polars LazyFrames for 100GB Parquet ETL

Data Science
Date:November 27, 2026
Topic:
Optimizing Polars LazyFrames for 100GB Parquet ETL
⏱ 3 min read

Processing 100GB of Parquet data on a single machine used to mean spinning up a Spark cluster or watching your OOM killer go to work. Polars LazyFrames change that equation entirely. By deferring execution and building a logical query plan, you can manipulate datasets larger than RAM using a fraction of the memory footprint—if you know how to drive the optimizer.

Why Lazy Evaluation Changes the Game

Eager DataFrames execute every line immediately. A 100GB filter-then-groupby operation materializes the filtered 80GB intermediate result before the aggregation runs. LazyFrames build a Directed Acyclic Graph (DAG) of expressions instead. The optimizer sees the full picture: it pushes predicates down to the Parquet scan, prunes columns, and fuses operations into a single streaming pass. No intermediate materialization. No memory spikes.

python
import polars as pl

# Lazy scan: zero I/O, zero memory
lf = pl.scan_parquet("s3://bucket/100gb-dataset/*.parquet")

# Build the plan
query = (
    lf.filter(pl.col("timestamp") > "2024-01-01")
      .select(["user_id", "event_type", "revenue"])
      .group_by("user_id")
      .agg(pl.col("revenue").sum().alias("ltv"))
      .sort("ltv", descending=True)
      .limit(100)
)

# Execute: single streaming pass
result = query.collect(streaming=True)

Predicate Pushdown: The First Line of Defense

Parquet stores min/max statistics in row group footers. When you filter on a partitioned column or a column with statistics, Polars skips entire row groups before decoding a single value. The catch: statistics only exist for the first 10 columns by default in many writers. Ensure your filter columns are written early in the schema or explicitly enable statistics for them.

💡
TipUse `pl.scan_parquet(..., hive_partitioning=True)` to auto-detect partition filters from directory structure (e.g., `year=2024/month=01/`).

Column Pruning: Read Only What You Need

A 100GB dataset with 200 columns might only need 5 for your analysis. Parquet is columnar—reading unused columns wastes I/O and decode cycles. Always call `.select()` immediately after the scan. Polars will only project required columns through the entire plan, and the Parquet reader will skip the rest entirely.

python
# Bad: reads all columns, then drops
lf = pl.scan_parquet("data/").filter(pl.col("a") > 1).select("b")

# Good: projects early, reader skips column 'a' decode for filtered rows
lf = pl.scan_parquet("data/").select("a", "b").filter(pl.col("a") > 1)

Streaming vs. Collect: Memory Profile Decisions

`.collect()` pulls the full result into memory. `.collect(streaming=True)` processes chunks sequentially, keeping memory constant regardless of output size. Use streaming for ETL sinks (writing Parquet, inserting to DB) or when the result exceeds RAM. Disable streaming only when you need the final DataFrame in memory for downstream Python operations.

MethodMemory UseUse Case
collect()O(result_size)Exploratory analysis, ML feature matrices
collect(streaming=True)O(chunk_size)ETL pipelines, large exports, DB inserts
sink_parquet()O(chunk_size)Direct Parquet output, zero memory copy

Join Optimization: Broadcast vs. Hash

Joining a 100GB fact table with a 10MB dimension table? Polars defaults to a hash join. Force a broadcast join by hinting the small side: `pl.col("dim").implode()` isn't valid syntax, but you can ensure the small table is eager and the large table lazy. Polars will broadcast the eager DataFrame to all threads.

python
# Eager small table, lazy large table -> broadcast join
dim = pl.read_parquet("dimensions.parquet")  # 10MB, eager
fact = pl.scan_parquet("facts/")               # 100GB, lazy

result = fact.join(dim, on="key", how="left").collect(streaming=True)

Inspect the Plan Before You Run

Never guess. Call `.show_graph()` or `.explain()` on the LazyFrame to see the optimized plan. Look for `PARQUET SCAN` with `projection` and `selection` fields populated. If you see `FILTER` nodes above the scan, predicate pushdown failed—check your expression syntax or Parquet statistics.

bash
# Terminal visualization
query.show_graph()

# Text plan
print(query.explain())
⚠️
WarningAvoid `pl.col("col").apply(lambda x: ...)` in lazy contexts. It breaks predicate pushdown, forces Python UDF execution, and kills vectorization. Use native expressions (`pl.when`, `pl.col.str`, `pl.col.dt`) instead.

Sink Directly to Disk

The ultimate optimization: never materialize the result in Python at all. Use `.sink_parquet()` or `.sink_csv()` on the LazyFrame. Polars writes the streaming output directly to disk in parallel, bypassing the GIL and memory allocation entirely. This is how you process 100GB on a 16GB laptop.

python
# Zero-memory ETL: Parquet -> Transform -> Parquet
(
    pl.scan_parquet("raw/")
    .filter(pl.col("status") == "completed")
    .select(["user_id", "amount", "date"])
    .group_by("user_id", "date")
    .agg(pl.col("amount").sum())
    .sink_parquet("processed/daily_revenue.parquet")
)

✦

Start your next 100GB job with `pl.scan_parquet`, chain `.select`, `.filter`, and `.group_by` natively, then `.sink_parquet()` with `streaming=True`. Open the query plan. Verify predicate pushdown and column projection. That's the entire loop. No cluster required.

Share𝕏 Twitterin LinkedInin Whatsapp