Sub topic 1
Why and when to migrate from pandas to Polars
Prerequisites
This chapter assumes you can:
- Work with pandas DataFrames and Series.
- Write basic Python, including lambdas and method chaining.
- Recognize common dtypes such as integers, floats, strings and datetimes.
- Install packages with pip.
Install Polars with pip install polars. Later chapters also use pyarrow for conversions.
What Polars is
Polars is a DataFrame library written in Rust, with a Python API. It runs work across multiple CPU cores and stores data in a columnar layout based on the Apache Arrow format. It also offers a lazy mode, where you describe a query first and Polars plans it before running.
pandas works differently in several ways. It mostly runs on one thread, keeps an index on every frame, and runs each step as soon as you call it.
Reasons to migrate
- Speed on larger data. Joins, group-bys and sorts can use all cores. Gains depend on the workload and are usually largest on big inputs.
- Memory. The lazy API can read only the columns and rows a query needs. Polars can also process some queries in streaming mode, in chunks, so data larger than RAM can sometimes be handled.
- Predictable code. There is no index to align on or reset. Missing values and dtypes follow stricter rules, so fewer results change silently.
- Readable pipelines. Expressions chain naturally, and frames are never modified in place.
Reasons to wait
- Small data. On a few thousand rows, pandas is often fast enough, and a rewrite buys little.
- Ecosystem coupling. Some libraries expect pandas objects, such as certain plotting, statistics and machine learning tools. You can convert at the boundary, but the conversion has a cost.
- Index-heavy code. Code built on MultiIndex, label alignment or
.locneeds a real redesign, not a line-by-line port. - Team familiarity. Expressions take time to learn, and the pandas habits you have will not all carry over.
A quick check
Compare the two libraries on your own data before deciding. The sketch below times one group-by in each.
import time
import pandas as pd
import polars as pl
pdf = pd.read_csv("orders.csv")
df = pl.read_csv("orders.csv")
start = time.perf_counter()
pdf.groupby("region")["qty"].sum()
print("pandas:", time.perf_counter() - start)
start = time.perf_counter()
df.group_by("region").agg(pl.col("qty").sum())
print("polars:", time.perf_counter() - start)Run it several times on realistic file sizes. One run includes warm-up effects, so it shows only a rough difference.
Deciding
The rest of this tutorial covers the mental model first, then each pandas idiom and its Polars equivalent.
Exercise
A nightly job reads a 20 GB CSV, filters rows, and sums one column per region. It takes 40 minutes and sometimes runs out of memory. A separate script plots a 2,000-row summary with a pandas-only library. Which one should you migrate first, and how do you keep the plotting script working?
Show answer
Migrate the nightly job first. It is slow, memory-bound and a good fit for the lazy API, which reads only the needed columns and rows. Leave the plotting script on pandas. Convert the small summary with to_pandas() at the boundary, which is cheap for 2,000 rows.