pola-rs/r-polars

Why is group_by() output always identically sorted?

Open

#1,409 opened on Jul 15, 2025

 (10 comments) (0 reactions) (0 assignees)R (41 forks)github user discovery
bughelp wanted

Repository metrics

Stars
 (568 stars)
PR merge metrics
 (Avg merge 14h 9m) (14 merged PRs in 30d)

Description

Moved from: https://github.com/eitsupi/neo-r-polars/issues/339


Python polars and old r-polars return dataframes with different row order, but the rewritten polars returns all dataframes identically sorted:

import polars as pl

n_obs = 6
df = pl.LazyFrame({
    "feature": ["b", "b", "c", "c", "a", "a"],
    "value": list(range(n_obs)),
})

for i in range(10):
    print(
        df.group_by("feature").agg([
            pl.col("value").mean(),
        ]).collect()
    )
df = pl$LazyFrame(
    feature = c("b", "b", "c", "c", "a", "a"),
    value = 1:6,
)

for (i in 1:10) {
  print(
    df$group_by("feature")$agg(
      pl$len(),
      pl$col("value")$mean(),
    )$collect()
  )
}

Contributor guide