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

pl.cum_sum_horizontal(...).struct.unnest() errors with StructFieldNotFoundError: literal #19765

Open
2 tasks done
rben01 opened this issue Nov 13, 2024 · 1 comment
Open
2 tasks done
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@rben01
Copy link
Contributor

rben01 commented Nov 13, 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], "b": [3, 4]})
df.select(pl.cum_sum_horizontal("a", "b").struct.unnest())

Log output

Traceback (most recent call last):
  File "/Users/rben01/test.py", line 44, in <module>
    df.select(pl.cum_sum_horizontal("a", "b").struct.unnest())
  File "/Users/rben01/Library/Caches/pypoetry/virtualenvs/base-wTklQcO3-py3.12/lib/python3.12/site-packages/polars/dataframe/frame.py", line 9024, in select
    return self.lazy().select(*exprs, **named_exprs).collect(_eager=True)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/rben01/Library/Caches/pypoetry/virtualenvs/base-wTklQcO3-py3.12/lib/python3.12/site-packages/polars/lazyframe/frame.py", line 2021, in collect
    return wrap_df(ldf.collect(callback))
                   ^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.StructFieldNotFoundError: literal

Issue description

This goes back to working if you do .struct.unnest() in a separate select():

df = pl.DataFrame({"a": [1, 2], "b": [3, 4]})
df.select(pl.cum_sum_horizontal("a", "b")).select(pl.col("cum_sum").struct.unnest())
shape: (2, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 4   │
│ 2   ┆ 6   │
└─────┴─────┘

Expected behavior

df.select(pl.cum_sum_horizontal("a", "b").struct.unnest())

should just work and return

shape: (2, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 1   ┆ 4   │
│ 2   ┆ 6   │
└─────┴─────┘

Installed versions

--------Version info---------
Polars:              1.13.0
Index type:          UInt32
Platform:            macOS-15.1-arm64-arm-64bit
Python:              3.12.7 (main, Oct  1 2024, 02:05:46) [Clang 16.0.0 (clang-1600.0.26.3)]
LTS CPU:             False

----Optional dependencies----
adbc_driver_manager  <not installed>
altair               5.4.1
cloudpickle          <not installed>
connectorx           <not installed>
deltalake            <not installed>
fastexcel            <not installed>
fsspec               2024.9.0
gevent               <not installed>
great_tables         <not installed>
matplotlib           3.9.1.post1
nest_asyncio         1.6.0
numpy                2.1.3
openpyxl             3.1.5
pandas               2.2.3
pyarrow              17.0.0
pydantic             2.9.2
pyiceberg            <not installed>
sqlalchemy           2.0.32
torch                2.4.1
xlsx2csv             <not installed>
xlsxwriter           <not installed>
@rben01 rben01 added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Nov 13, 2024
@cmdlineluser
Copy link
Contributor

It seems the literal used for acc= in the underlying cum_fold call becomes part of the output dtype.

return F.cum_fold(F.lit(0).cast(UInt32), lambda a, b: a + b, exprs_wrapped).alias(

(df
  .lazy()
  .select(pl.cum_fold(0, lambda x, y: x + y, pl.all()))
  .collect_schema()
)
# Schema([('cum_fold', Struct({'a': Int64, 'b': Int64, 'literal': Int64}))])
#                                                       ^^^^^^^
(df
  .lazy()
  .select(pl.cum_fold(0, lambda x, y: x + y, pl.all()))
  .collect()
  .collect_schema()
)
# Schema([('cum_fold', Struct({'a': Int64, 'b': Int64}))])

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

No branches or pull requests

2 participants