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

Nested struct column is null after pivoting DataFrame #17065

Closed
2 tasks done
edm1 opened this issue Jun 19, 2024 · 1 comment · Fixed by #20680
Closed
2 tasks done

Nested struct column is null after pivoting DataFrame #17065

edm1 opened this issue Jun 19, 2024 · 1 comment · Fixed by #20680
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@edm1
Copy link

edm1 commented Jun 19, 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

import polars as pl

df = pl.DataFrame(
    {
        "foo": ["one", "two", "one", "two"],
        "bar": ["x", "x", "y", "y"],
        "baz": [
            {"a": 1, "b": {"c": 2}},
            {"a": 3, "b": {"c": 4}},
            {"a": 5, "b": {"c": 6}},
            {"a": 7, "b": {"c": 8}},
        ]
    }
)

piv = df.pivot(index="foo", columns="bar", values="baz")

print(piv)

Log output

shape: (2, 3)
┌─────┬────────────┬────────────┐
│ foo ┆ x          ┆ y          │
│ --- ┆ ---        ┆ ---        │
│ str ┆ struct[2]  ┆ struct[2]  │
╞═════╪════════════╪════════════╡
│ one ┆ {1,{null}} ┆ {5,{null}} │
│ two ┆ {3,{null}} ┆ {7,{null}} │
└─────┴────────────┴────────────┘

Issue description

Pivotting values containing a nested struct causes the nested contents to become null.

Expected behavior

Expected output

shape: (2, 3)
┌─────┬────────────┬────────────┐
│ foo ┆ x          ┆ y          │
│ --- ┆ ---        ┆ ---        │
│ str ┆ struct[2]  ┆ struct[2]  │
╞═════╪════════════╪════════════╡
│ one ┆ {1,{2}}    ┆ {5,{6}}    │
│ two ┆ {3,{4}}    ┆ {7,{8}}    │
└─────┴────────────┴────────────┘

Installed versions

--------Version info---------
Polars:               0.20.31
Index type:           UInt32
Platform:             Linux-5.15.0-1064-azure-x86_64-with-glibc2.36
Python:               3.12.3 (main, May 14 2024, 07:23:41) [GCC 12.2.0]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               2024.6.0
gevent:               <not installed>
hvplot:               <not installed>
matplotlib:           3.9.0
nest_asyncio:         <not installed>
numpy:                1.26.4
openpyxl:             <not installed>
pandas:               <not installed>
pyarrow:              16.1.0
pydantic:             2.7.3
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
torch:                2.3.0+cu121
xlsx2csv:             0.8.2
xlsxwriter:           <not installed>
@edm1 edm1 added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Jun 19, 2024
@cmdlineluser
Copy link
Contributor

Can reproduce.

In case it may be a useful datapoint for debugging, transpose also seems to lose the inner values.

>>> df.drop('foo').transpose(column_names='bar')
# shape: (1, 4)
# ┌────────────┬────────────┬────────────┬────────────┐
# │ x          ┆ x          ┆ y          ┆ y          │
# │ ---        ┆ ---        ┆ ---        ┆ ---        │
# │ struct[2]  ┆ struct[2]  ┆ struct[2]  ┆ struct[2]  │
# ╞════════════╪════════════╪════════════╪════════════╡
# │ {1,{null}} ┆ {3,{null}} ┆ {5,{null}} ┆ {7,{null}} │
# └────────────┴────────────┴────────────┴────────────┘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants