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

group_by(explode()) produces different output than explode().group_by() #15984

Open
2 tasks done
lightningboltemoji opened this issue May 1, 2024 · 2 comments
Open
2 tasks done
Labels
bug Something isn't working P-low Priority: low python Related to Python Polars

Comments

@lightningboltemoji
Copy link

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({"a":[1], "b":[[2,3,4]], "c":[5]})
print(f"{df}\n")
print(f"{df.group_by('a', pl.col('b').explode()).sum()}\n")
print(f"{df.explode('b').group_by('a', 'b').sum()}")

Log output

% POLARS_VERBOSE=1 python3 report.py
shape: (1, 3)
┌─────┬───────────┬─────┐
│ a   ┆ b         ┆ c   │
│ --- ┆ ---       ┆ --- │
│ i64 ┆ list[i64] ┆ i64 │
╞═════╪═══════════╪═════╡
│ 1   ┆ [2, 3, 4] ┆ 5   │
└─────┴───────────┴─────┘

keys/aggregates are not partitionable: running default HASH AGGREGATION
shape: (1, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1   ┆ 2   ┆ 5   │
└─────┴─────┴─────┘

DATAFRAME < 1000 rows: running default HASH AGGREGATION
shape: (3, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1   ┆ 3   ┆ 5   │
│ 1   ┆ 4   ┆ 5   │
│ 1   ┆ 2   ┆ 5   │
└─────┴─────┴─────┘

Issue description

When using explode inside of a group_by, I was expecting to get the same result as exploding then grouping. Instead, it feels like only the first element of b was exploded (2) and the other values (3, 4) were dropped.

I noticed that my output shrunk while refactoring and traced it back to this. No reason for using one over the other. Sorry in advance if I'm thinking about this wrong.

Expected behavior

Both forms produce the bottom dataframe (referring to log output).

Installed versions

--------Version info---------
Polars:               0.20.23
Index type:           UInt32
Platform:             macOS-14.4.1-arm64-arm-64bit
Python:               3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               <not installed>
gevent:               <not installed>
hvplot:               <not installed>
matplotlib:           <not installed>
nest_asyncio:         <not installed>
numpy:                <not installed>
openpyxl:             <not installed>
pandas:               <not installed>
pyarrow:              <not installed>
pydantic:             <not installed>
pyiceberg:            <not installed>
pyxlsb:               <not installed>
sqlalchemy:           <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>
@lightningboltemoji lightningboltemoji added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels May 1, 2024
@cmdlineluser
Copy link
Contributor

Should this raise a ShapeError?

If the explode is the only group key, it raises:

df.group_by(pl.col("b").explode()).all()
# ShapeError: series used as keys should have the same length as the DataFrame

@deanm0000
Copy link
Collaborator

Should this raise a ShapeError?

I think so.

@deanm0000 deanm0000 added P-low Priority: low and removed needs triage Awaiting prioritization by a maintainer labels May 7, 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-low Priority: low python Related to Python Polars
Projects
Status: Ready
Development

No branches or pull requests

3 participants