Sub topic 18
Validating Migrated Results
This chapter assumes you can write basic Polars queries, convert frames with from_pandas and to_pandas, and have a pandas pipeline that you are porting. Validation means proving that the Polars version produces the same data as the pandas version before you remove the old code.
Why Results Drift
A migrated query can look right and still differ from the original. The common causes are:
- Row order. pandas
groupbysorts keys by default. Polarsgroup_bydoes not guarantee any order. - Missing values. pandas often stores missing values as
NaN. Polars separatesnullfromNaN. - Dtypes. An integer column with gaps becomes
float64in pandas. In Polars it stays an integer with nulls. - Index. pandas carries an index that Polars drops, so
reset_indexmatters when you compare.
Comparing Two Frames
Convert the pandas result into Polars, then call assert_frame_equal. It raises an error that names the first difference it finds.
import pandas as pd
import polars as pl
from polars.testing import assert_frame_equal
pandas_result = (
pd.DataFrame({"region": ["N", "S", "N"], "total": [10.0, 20.0, 30.0]})
.groupby("region", as_index=False)["total"]
.sum()
)
polars_result = (
pl.DataFrame({"region": ["N", "S", "N"], "total": [10.0, 20.0, 30.0]})
.group_by("region")
.agg(pl.col("total").sum())
)
expected = pl.from_pandas(pandas_result.reset_index(drop=True))
assert_frame_equal(polars_result, expected, check_row_order=False)
print("results match")check_row_order=False is needed here because group_by may return the groups in any order. If your original code relied on sorted output, sort explicitly in Polars and keep the default check_row_order=True. That way the test also checks the ordering you depend on.
Controlling Strictness
assert_frame_equal has options that let you decide what counts as equal.
| Option | Default | Use it when |
|---|---|---|
| `check_row_order` | `True` | Row order is not part of the contract |
| `check_column_order` | `True` | Column order does not matter |
| `check_dtypes` | `True` | You accept a known dtype change, such as `int64` to `int32` |
| `check_exact` | `False` | Set to `True` to require bit-identical floats |
| `abs_tol` and `rel_tol` | small values | Floating-point sums differ by rounding |
Loosen one option at a time, and only for a difference you understand. If you set check_dtypes=False just to make a failing test pass, you hide the null versus NaN problem this chapter warns about.
A Validation Workflow
Follow these steps for each function you migrate:
- Save a fixed sample of real input, including edge cases such as empty groups, missing values and duplicate keys.
- Run the pandas function and keep its output as the reference.
- Run the Polars function on the same input.
- Compare with
assert_frame_equaland the strictest options that still make sense. - Keep the comparison as a test until you delete the pandas version.
Also compare shape and null counts on the full dataset, because a sample can miss rare cases. Check df.height and df.null_count() on both sides.
Exercise
Your Polars result and the pandas reference contain the same groups, but assert_frame_equal fails. Name two likely causes and the option or step that addresses each.
Show answer
One cause is row order, because Polars group_by does not sort. Pass check_row_order=False, or sort both frames by the key. Another cause is a dtype difference, such as pandas returning float64 where Polars keeps an integer with nulls. Fix the migrated code to produce the right dtype, or use check_dtypes=False only if you accept that change. Floating-point rounding is a third cause. Use abs_tol or rel_tol.