SyncAI.news, a Varaisys broadcasting
3 Polars Tricks for High-Performance Data Manipulation
MM

Matthew Mayo

· 1 min read

EngineeringKDnuggets

3 Polars Tricks for High-Performance Data Manipulation

Polars gets its speed from two places: its expression engine written and executing in Rust across every core at its disposal, and its query optimizer that rewrites your work before any of it runs. Almost every slow Polars script lacks in terms of one of those two. The awkward part is that the fast version and the slow version end up looking nearly identical on the page. Here are three places that happens.

The examples run against a month of NYC yellow taxi trips, published by the TLC as Parquet. Download it first:

curl -O https://d37ci6vzurychx.cloudfront.net/trip-data/yellow_tripdata_2026-01.parquet

Everything below was checked against Polars 1.44.2.

Trick 1: Scanning a File Instead of Reading It

pl.read_parquet pulls an entire file's contents into memory and then lets you filter it. pl.scan_parquet hands back a LazyFrame instead; this records what you asked for without doing any of it. That gap is where the optimizer earns its keep: it pushes your filter and your column list down to the scan itself, so the narrowing happens as the file is read rather than after. The rows you threw away were never decoded, and thus computation was never wasted upon them.

import polars as pl

q = (
    pl.scan_parquet("yellow_tripdata_2026-01.parquet")
    .filter(pl.col("fare_amount") > 50)
    .select("PULocationID", "tip_amount")
    .group_by("PULocationID")
    .agg(pl.col("tip_amount").mean())
)

# This explains the plan, not the data
print(q.explain())
df = q.collect()

Nothing becomes permanent until collect(). explain() prints the plan the optimizer has built, and you can read the pushed-down predicate and the trimmed column list:

AGGREGATE[maintain_order: false]
  [col("tip_amount").mean()] BY [col("PULocationID")]
  FROM
  simple π 2/2 ["PULocationID", "tip_amount"]
    Parquet SCAN [yellow_tripdata_2026-01.parquet]
    PROJECT 3/20 COLUMNS
    SELECTION: col("fare_amount") > 50.0
    ESTIMATED ROWS: 3724889

Trick 2: Per-Group Values Without the Group-By Round Trip

Output:

Output:

Original source

This story was published by KDnuggets and written by Matthew Mayo. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on kdnuggets.com

Similar News