pandas mask + sort_values is ~6.54× slower than polars filter + sort for filtering then sorting
Two operations chained, timed as one.
Result
| Run | pandas mask + sort_values | polars filter + sort | Ratio |
|---|---|---|---|
| Run 1 | 67.34 | 10.71 | 6.29× |
| Run 2 | 72.9 | 11.15 | 6.54× |
| Run 3 | 67.95 | 10.3 | 6.59× |
Spread over 3 runs: pandas mask + sort_values CV 4.4% / polars filter + sort CV 3.9% / ratio CV 2.5%
The two calls
df[df['v'] > 0.5].sort_values('v') df.filter(pl.col('v') > 0.5).sort('v') Conditions
Half the rows survive the filter, then get sorted ascending.
Where this number does not apply
Sorting a column that is already sorted is much cheaper for both sides.
Reproduce
cd bench && python run.py --run 1 --out ../data/run1.json