Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Varying quantile by group is broken #20951

Open
orlp opened this issue Jan 28, 2025 · 1 comment
Open

Varying quantile by group is broken #20951

orlp opened this issue Jan 28, 2025 · 1 comment
Labels
accepted Ready for implementation bug Something isn't working P-high Priority: high python Related to Python Polars

Comments

@orlp
Copy link
Collaborator

orlp commented Jan 28, 2025

Consider the following example:

df = pl.DataFrame(
    {
        "value": [1, 2, 1, 2],
        "quantile": [0, 0, 1, 1],
    }
)
df.group_by(pl.col.quantile).agg(pl.col.value.quantile(pl.col.quantile.first()))

Output:

shape: (2, 2)
┌──────────┬───────┐
│ quantile ┆ value │
│ ---      ┆ ---   │
│ i64      ┆ f64   │
╞══════════╪═══════╡
│ 0        ┆ 1.0   │
│ 1        ┆ 1.0   │
└──────────┴───────┘

I would expect the value of the quantile = 1 group to be 2.0, not 1.0.

@orlp orlp added bug Something isn't working python Related to Python Polars accepted Ready for implementation P-high Priority: high labels Jan 28, 2025
@npennequin
Copy link

I also ran into this issue. Was about to open an issue.

I found that using map_groups returns the correct results, but it's slower:

(
    df
    .group_by("quantile")
    .map_groups(lambda group_df: group_df.select(
        pl.col.quantile.first(),
        pl.col.value.quantile(pl.col.quantile.first())
    ))
)

Output:

shape: (2, 2)
┌──────────┬───────┐
│ quantile ┆ value │
│ ---      ┆ ---   │
│ i64      ┆ f64   │
╞══════════╪═══════╡
│ 0        ┆ 1.0   │
│ 1        ┆ 2.0   │
└──────────┴───────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation bug Something isn't working P-high Priority: high python Related to Python Polars
Projects
None yet
Development

No branches or pull requests

2 participants