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

DataFrame.pivot returns incorrect results when multi-index contains a struct #14307

Closed
2 tasks done
MarcoGorelli opened this issue Feb 6, 2024 · 0 comments · Fixed by #14438
Closed
2 tasks done

DataFrame.pivot returns incorrect results when multi-index contains a struct #14307

MarcoGorelli opened this issue Feb 6, 2024 · 0 comments · Fixed by #14438
Assignees
Labels
bug Something isn't working P-medium Priority: medium python Related to Python Polars

Comments

@MarcoGorelli
Copy link
Collaborator

MarcoGorelli commented Feb 6, 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": [1, 2, 1],
        "b": [{"a": 1}, {"a": 1}, {"a": 2}],
        "c": ["x", "y", "y"],
        "d": [1, 1, 3],
    }
)
print(df.pivot(index=("b", "d"), values="a", columns="c"))

Log output

shape: (3, 4)
┌───────────┬─────┬──────┬──────┐
│ b         ┆ d   ┆ x    ┆ y    │
│ ---       ┆ --- ┆ ---  ┆ ---  │
│ struct[1] ┆ i64 ┆ i64  ┆ i64  │
╞═══════════╪═════╪══════╪══════╡
│ {null}    ┆ 1   ┆ 1    ┆ null │
│ {null}    ┆ 1   ┆ null ┆ 2    │
│ {null}    ┆ 3   ┆ null ┆ 1    │
└───────────┴─────┴──────┴──────┘

Issue description

Similar to #14101, but it goes down a different branch so I think it deserves a separate issue to make sure that both parts get addressed

Expected behavior

I think it should be

shape: (2, 4)
┌──────────┬─────┬──────┬─────┐
│ b        ┆ d   ┆ x    ┆ y   │
│ ---      ┆ --- ┆ ---  ┆ --- │
│ struct[1]┆ i64 ┆ i64  ┆ i64 │
╞══════════╪═════╪══════╪═════╡
│ {1}      ┆ 1   ┆ 1    ┆ 2   │
│ {2}      ┆ 3   ┆ null ┆ 1   │
└──────────┴─────┴──────┴─────┘

Installed versions

--------Version info---------
Polars:               0.20.7
Index type:           UInt32
Platform:             Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Python:               3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          3.0.0
connectorx:           <not installed>
deltalake:            <not installed>
fsspec:               2023.12.2
gevent:               <not installed>
hvplot:               0.9.1
matplotlib:           3.8.2
numpy:                1.26.3
openpyxl:             <not installed>
pandas:               2.2.0
pyarrow:              15.0.0
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           2.0.25
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>```

</details>
@MarcoGorelli MarcoGorelli added bug Something isn't working python Related to Python Polars needs triage Awaiting prioritization by a maintainer P-medium Priority: medium and removed needs triage Awaiting prioritization by a maintainer labels Feb 6, 2024
@MarcoGorelli MarcoGorelli self-assigned this Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P-medium Priority: medium python Related to Python Polars
Projects
Archived in project
1 participant