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

json_decode followed by .struct.field(<fld>) raises StructFieldNotFoundError in lazy mode #21073

Open
2 tasks done
alexander-beedie opened this issue Feb 4, 2025 · 0 comments
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@alexander-beedie
Copy link
Collaborator

alexander-beedie commented Feb 4, 2025

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

🟢 Eager (works)

import polars as pl

df = pl.DataFrame({
  "info": ['{"event":"UPDATE","event_id":"123456","provider":"SOMEBODY"}']
})
df = df.select(pl.col("info").str.json_decode())
# shape: (1, 1)
# ┌────────────────────────────────┐
# │ info                           │
# │ ---                            │
# │ struct[3]                      │
# ╞════════════════════════════════╡
# │ {"UPDATE","123456","SOMEBODY"} │
# └────────────────────────────────┘

df.select(pl.col("info").struct.field("provider"))
# shape: (1, 1)
# ┌──────────┐
# │ provider │
# │ ---      │
# │ str      │
# ╞══════════╡
# │ SOMEBODY │
# └──────────┘

🔴 Lazy (fails)

lf = pl.LazyFrame({
  "info": ['{"event":"UPDATE","event_id":"123456","provider":"SOMEBODY"}']
})
lf = lf.select(pl.col("info").str.json_decode())
lf.collect()
# shape: (1, 1)
# ┌────────────────────────────────┐
# │ info                           │
# │ ---                            │
# │ struct[3]                      │
# ╞════════════════════════════════╡
# │ {"UPDATE","123456","SOMEBODY"} │
# └────────────────────────────────┘

lf.select(pl.col("info").struct.field("provider")).collect()
# polars.exceptions.StructFieldNotFoundError: provider

Log output

Traceback (most recent call last):
  lf.select(pl.col("info").struct.field("provider")).collect()
File "/.../polars/lazyframe/frame.py", line 2056, in collect
  return wrap_df(ldf.collect(callback))
                 ^^^^^^^^^^^^^^^^^^^^^
polars.exceptions.StructFieldNotFoundError: provider
Resolved plan until failure:
---> FAILED HERE RESOLVING 'select' <---
SELECT [col("info").str.json_decode()] FROM
DF ["info"]; PROJECT */1 COLUMNS

Issue description

Lazy mode raises an error when unpacking a struct field following json_decode. Note: disabling the optimiser (.collect(no_optimization=True))does not resolve/avoid the error.

Expected behavior

Should successfully identify/return the requested struct field, as does eager mode.

Installed versions

--------Version info---------
Polars: 1.21.0
Index type: UInt32
Platform: macOS-15.2-arm64-arm-64bit
Python: 3.12.8 (main, Dec 3 2024, 18:42:41) [Clang 16.0.0 (clang-1600.0.26.4)]
LTS CPU: False
----Optional dependencies----
Azure CLI
adbc_driver_manager 1.4.0
altair
azure.identity
boto3 1.36.3
cloudpickle
connectorx 0.3.3
deltalake
fastexcel 0.12.1
fsspec 2024.12.0
gevent
google.auth 2.37.0
great_tables 0.16.1
matplotlib
numpy 2.0.2
openpyxl
pandas 2.2.3
pyarrow 19.0.0
pydantic 2.9.1
pyiceberg
sqlalchemy 1.4.54
torch 2.5.1
xlsx2csv
xlsxwriter 3.2.2

Replace this line with the output of pl.show_versions(). Leave the backticks in place.
@alexander-beedie alexander-beedie added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Feb 4, 2025
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

1 participant