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

rank().over() replaces wrongly assigns rank=1 to null values in the case that the over() group has a size of 1. #17570

Closed
2 tasks done
bfeif opened this issue Jul 11, 2024 · 0 comments · Fixed by #18252
Closed
2 tasks done
Assignees
Labels
accepted Ready for implementation bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@bfeif
Copy link
Contributor

bfeif commented Jul 11, 2024

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

df = (
    pl.DataFrame({
        "a": [2.1, 4, None, 2.5],
        "b": ["j", "j", "k", "l"]
    })
    .with_columns(
        pl.col("a").rank().over("b")
        .name.suffix("_rank_over_b")
    )
)
print(df)

Log output

shape: (4, 3)
┌──────┬─────┬────────┐
│ a    ┆ b   ┆ a_rank │
│ ---  ┆ --- ┆ ---    │
│ f64  ┆ str ┆ f64    │
╞══════╪═════╪════════╡
│ 2.1  ┆ j   ┆ 1.0    │
│ 4.0  ┆ j   ┆ 2.0    │
│ null ┆ k   ┆ 1.0    │
│ 2.5  ┆ l   ┆ 1.0    │
└──────┴─────┴────────┘


### Issue description

Title explains everything.

### Expected behavior

Behavior should be as it is normally when the `over()` group has a size larger than 1:

```python
df = (
    pl.DataFrame({
        "a": [2.1, 4, None, 2.5],
        "b": [
            "j",
            "j",
            "l",  # Note: the third element has switched from the issue example, from `"k"` to `"l"`
            "l"
        ]
    })
    .with_columns(
        pl.col("a").rank().over("b")
        .name.suffix("_rank_over_b")
    )
)
print(df)
shape: (4, 3)
┌──────┬─────┬───────────────┐
│ a    ┆ b   ┆ a_rank_over_b │
│ ---  ┆ --- ┆ ---           │
│ f64  ┆ str ┆ f64           │
╞══════╪═════╪═══════════════╡
│ 2.1  ┆ j   ┆ 1.0           │
│ 4.0  ┆ j   ┆ 2.0           │
│ null ┆ l   ┆ null          │
│ 2.5  ┆ l   ┆ 1.0           │
└──────┴─────┴───────────────┘

Installed versions

--------Version info---------
Polars:               1.1.0
Index type:           UInt32
Platform:             macOS-14.5-arm64-arm-64bit
Python:               3.12.3 (main, Jun 12 2024, 23:52:12) [Clang 12.0.5 (clang-1205.0.22.11)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            0.10.4
fsspec:               <not installed>
gevent:               <not installed>
great_tables:         <not installed>
hvplot:               <not installed>
matplotlib:           <not installed>
nest_asyncio:         1.6.0
numpy:                2.0.0
openpyxl:             <not installed>
pandas:               <not installed>
pyarrow:              16.1.0
pydantic:             <not installed>
pyiceberg:            <not installed>
sqlalchemy:           <not installed>
torch:                <not installed>
xlsx2csv:             0.8.2
xlsxwriter:           <not installed>
@bfeif bfeif added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jul 11, 2024
@c-peters c-peters added the accepted Ready for implementation label Aug 19, 2024
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 needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants