pandas groupby + agg(dict) is ~5.32× slower than polars group_by + agg for three aggregates per group
Same three aggregates on the same groups.
Result
| Run | pandas groupby + agg(dict) | polars group_by + agg | Ratio |
|---|---|---|---|
| Run 1 | 182.12 | 38.0 | 4.79× |
| Run 2 | 214.04 | 36.89 | 5.80× |
| Run 3 | 187.87 | 35.3 | 5.32× |
Spread over 3 runs: pandas groupby + agg(dict) CV 8.7% / polars group_by + agg CV 3.7% / ratio CV 9.5%
The two calls
df.groupby('g').agg(v_sum=('v','sum'), v_mean=('v','mean'), k_max=('k','max')) df.group_by('g').agg(pl.col('v').sum(), pl.col('v').mean(), pl.col('k').max()) Conditions
1,000 groups, 400,000 rows.
Where this number does not apply
Absolute times here are small, so the ratio moves more between runs than the large operations do.
Reproduce
cd bench && python run.py --run 1 --out ../data/run1.json