Skip to content

polars/polars-sort-then-head

Pack Severity Fix Versions
Polars info unsafe, with xpd check --fix --unsafe-fixes All versions

For a small number of rows, top_k or bottom_k can avoid a full sort. Sort the small result if its output order matters.

df.sort("x", descending=True).head(5)
df.top_k(5, by="x").sort("x", descending=True)

Top-k was 2.51x and 3.96x faster on shuffled keys, and slower on already sorted keys.

Use xpd check --fix --unsafe-fixes to rewrite sort(...).head(...) to top_k or bottom_k when output order does not matter.

For a small number of rows, top_k or bottom_k can avoid a full sort. Sort the small result if its output order matters.

See all rules in the Polars pack.