Lazy Evaluation in Polars vs Immediate Execution in Pandas
Published:
• Last updated:
• By Jeferson Peter
Polars & Pandas
A common scenario in data processing is chaining many operations.
In Pandas, each step runs immediately. Polars offers a lazy mode that builds the query first, then executes when you call.collect().
Example data
import pandas as pd
import polars as pl
data = {"name": ["Alice", "Bob", "Charlie"], "score": [85, 92, 78]}
df_pd = pd.DataFrame(data)
df_pl = pl.DataFrame(data)
Pandas: eager execution
result_pd = df_pd[df_pd["score"] > 80][["name"]]
print(result_pd)
# name
# 0 Alice
# 1 Bob
Each step executes immediately.
Polars: lazy evaluation
lazy_query = (
df_pl.lazy()
.filter(pl.col("score") > 80)
.select("name")
)
print(lazy_query) # shows query plan
print(lazy_query.collect())
# shape: (2, 1)
# ┌───────┐
# │ name │
# │ --- │
# │ str │
# ╞═══════╡
# │ Alice │
# │ Bob │
# └───────┘
Conclusion
- Pandas: operations are eager — run step by step.
- Polars: lazy mode builds a plan, optimized before execution.
- Lazy evaluation is efficient for complex pipelines and large data.