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

LazyFrame has incorrect schema following aggregation of an exploded list column expression #19562

Closed
2 tasks done
nsfinkelstein opened this issue Oct 31, 2024 · 1 comment · Fixed by #19629
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

@nsfinkelstein
Copy link

nsfinkelstein commented Oct 31, 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.LazyFrame({ 'a': [0, 0, 1, 2], 'b': [['a'], ['b'], ['c'], ['d', 'e']]})
df1 = df.group_by('a').agg(pl.col('b').explode())
df2 = df1.collect()

print(df1.collect_schema())
print(df2.collect_schema())
assert df1.collect_schema() == df2.collect_schema(), 'collecting should not change schema'

Log output

Schema([('a', Int64), ('b', String)])
Schema([('a', Int64), ('b', List(String))])
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[1], line 9
      7 print(df1.collect_schema())
      8 print(df2.collect_schema())
----> 9 assert df1.collect_schema() == df2.collect_schema(), 'collecting should not change schema'

AssertionError: collecting should not change schema

Issue description

The LazyFrame seems to lose track of the correct schema when aggregating an exploded list expression. This is resolved upon calling collect, however it can lead to some difficult bugs if there are lazy operations following the aggregation.

Expected behavior

Calling collect should not change the schema, which in this case would lead to no assertion error. In particular the LazyFrame schema should know that c has a List type.

Installed versions

--------Version info---------
Polars:              1.12.0
Index type:          UInt32
Platform:            Linux-5.10.219-208.866.amzn2.x86_64-x86_64-with-glibc2.35
Python:              3.11.9 (main, Sep 23 2024, 17:32:51) [GCC 11.4.0]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               <not installed>
cloudpickle          3.1.0
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2024.9.0
gevent               <not installed>
great_tables         0.13.0
matplotlib           3.9.2
nest_asyncio         1.6.0
numpy                1.26.4
openpyxl             <not installed>
pandas               2.2.3
pyarrow              17.0.0
pydantic             2.9.2
pyiceberg            <not installed>
sqlalchemy           2.0.35
torch                2.5.0+cu124
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@nsfinkelstein nsfinkelstein added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Oct 31, 2024
@nsfinkelstein nsfinkelstein changed the title Call to collect changes the schema LazyFrame has incorrect schema following aggregation of an exploded list column expression Oct 31, 2024
@ritchie46
Copy link
Member

Can you take a look at this one @nameexhaustion?

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
4 participants