Pandas is slow? Try Polars.
TL;DR: Pandas uses too much memory on big files. Polars is 10 to 20 times faster and uses less memory. Try it on a cloud computer.
A small story
You have a small file. Pandas loads it fast. Everything works.
Then you get a 20GB file. Pandas tries to load all of it. Your computer runs out of memory. The notebook crashes.
Why?
Pandas is like one person carrying all boxes at once. Polars is like a team. Each person carries one box. They work together. They finish faster. They do not get tired.
What is Polars?
Polars is written in Rust. Rust is fast. It uses Apache Arrow. Arrow is a way to store data that computers like.
Pandas uses one CPU core. Polars uses all your CPU cores. If your cloud computer has 16 cores, Polars uses all 16. Pandas uses 1.
Two important things:
- Lazy: Polars makes a plan first. It checks: filter first, then group. It finds the fastest way.
- Streaming: Polars reads data in small pieces. It does not need all data in memory. So a 100GB file can work on a small computer.
Code: Pandas vs Polars
Pandas:
import pandas as pd
df = pd.read_parquet("gs://my-bucket/events_20gb.parquet")
result = df[df["event_type"] == "purchase"].groupby("user_id")["amount"].sum()
This loads everything. Memory goes up. It may crash.
Polars:
import polars as pl
lf = pl.scan_parquet("gs://my-bucket/events_20gb.parquet")
query = (
lf
.filter(pl.col("event_type") == "purchase")
.group_by("user_id")
.agg(pl.col("amount").sum())
)
result = query.collect(streaming=True)
scan_parquet reads only file info. collect(streaming=True) does the work in pieces. Filter happens first. Only needed columns are read. Memory stays low.
Three big things
1. Memory and streaming
Use streaming=True. Polars processes chunks. A 100GB file works.
query = (
pl.scan_parquet("gs://my-bucket/100gb/*.parquet")
.filter(pl.col("date") >= "2024-01-01")
.group_by("region")
.agg(pl.col("revenue").sum())
)
result = query.collect(streaming=True)
2. Lazy plan
Polars thinks first. You can see the plan.
print(query.explain())
This shows which columns Polars will read and where it will filter. Pandas reads everything.
3. Cloud
Polars reads directly from Google Cloud Storage. No download needed.
df = pl.read_parquet("gs://my-bucket/data.parquet")
lf = pl.scan_parquet("gs://my-bucket/data/*.parquet")
Just run gcloud auth application-default login. Then it works.
Measure time
import time
start = time.perf_counter()
result = query.collect(streaming=True)
print(time.perf_counter() - start)
Compare with Pandas. On a 5GB file:
- Pandas: 94 seconds
- Polars: 6 seconds
- About 15 times faster
When to use Pandas
Small data under 1GB? Pandas is fine.
Special Pandas functions? Keep Pandas.
Otherwise, use Polars.
Switch between them:
df_pl = pl.from_pandas(df_pd)
df_pd = result_pl.to_pandas()
Try it
Open a cloud notebook. Put a Parquet file in cloud storage. Write the same query in Pandas and Polars. Time both.
Start with pip install polars. You will learn in an hour.
Your notebook will stop waiting. It will answer fast.