Lesson 9 of 9 · 27m
When to Escape to SQL or polars
How to choose pandas, SQL, or polars based on data location, memory model, execution semantics, and team operability.
pandas is a wonderful in-memory analysis tool and a mediocre place to pretend you have a database. Senior data work is largely boundary work: deciding where filtering, joining, aggregation, and correctness guarantees should live. The answer is not ideological. It depends on where the data already is, how big it is, what execution model you need, and who has to operate the result at 2 a.m.
If the data is in a database, start with SQL
Databases already know how to filter, join, aggregate, enforce constraints, and use indexes without loading everything into Python. A SQL query that returns a 200-column raw extract so pandas can group it locally is usually backwards. Push reduction down:
select customer_id,
date_trunc('day', created_at) as day,
sum(amount) as revenue
from events
where created_at >= date '2026-01-01'
group by 1, 2;Then use pandas for reshaping, statistical detail, visualization, and business logic that is genuinely easier in Python. SQL is also the correctness layer: uniqueness constraints and transactions do not exist in a CSV pipeline.
polars when the problem is pandas execution, not pandas semantics
polars is a strong fit when you like dataframes but need lazy execution, better memory behavior, multithreading, predicate pushdown, and streaming over larger-than-memory datasets. A rough translation:
import polars as pl
out = (
pl.scan_parquet("events/*.parquet")
.filter(pl.col("created_at") >= pl.date(2026, 1, 1))
.group_by("customer_id")
.agg(pl.col("amount").sum().alias("revenue"))
.collect()
)The key difference is scan plus lazy planning: polars can optimize the whole query before execution, project only needed columns, and stream chunks. In pandas, operations are mostly eager and each line executes immediately.
Do not migrate because benchmarks are shiny
Migration costs live in semantics: index alignment disappears, missing-value behavior differs, datetime APIs differ, and team intuition stops transferring. pandas is still excellent for exploration, small-to-medium batch work, ecosystem compatibility, and messy one-off transformations. polars is compelling when you repeatedly hit memory ceilings or want a dataframe-shaped query engine. SQL is compelling when the data already lives there or correctness constraints matter.
The production decision matrix
- Fits comfortably in memory, interactive analysis → pandas.
- Large batch dataframe transformations, repeated pipelines → polars.
- Source of truth in PostgreSQL/warehouse → SQL first, dataframe second.
- Multiple teams, strict correctness, concurrency → database constraints, not dataframe hope.
- Truly distributed scale → neither single-node tool is the answer.
Field notes
- The tool boundary should reduce data early, not just move the same bytes around.
- pandas indexes are powerful but unusual; do not expect SQL or polars people to infer them.
- polars lazy mode is where most benefits live; eager polars is only part of the story.
- A warehouse SQL query can be more reproducible than a notebook because state is explicit.
- Choose the tool your team can debug under incident pressure, not the one with the best benchmark chart.
Was this lesson helpful?