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

df.explode() + df.unnest() causes PanicException: called Result::unwrap() on an Err value #6060

Closed
2 tasks done
TimovNiedek opened this issue Jan 5, 2023 · 1 comment · Fixed by #6063
Closed
2 tasks done
Labels
bug Something isn't working python Related to Python Polars

Comments

@TimovNiedek
Copy link

TimovNiedek commented Jan 5, 2023

Polars version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of Polars.

Issue description

I'm running into an unexpected error when I have a dataframe with a column of a list of structs that I first explode, and then unnest. Whenever I try to select any of the unnested fields, I get the following exception:

pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: NotFound(Owned("users"))

Reproducible example

import polars as pl

df = pl.DataFrame(
    [
        {"collection": 0, "users": [{"email": "test_0@test.com"}]},
        {"collection": 1, "users": [{"email": "test_1@test.com"}]},
    ]
).lazy()

df = df.explode("users")
df = df.unnest("users")

df.select("email").collect(
    projection_pushdown=True,
)

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: NotFound(Owned("users"))', /Users/runner/work/polars/polars/polars/polars-lazy/polars-plan/src/utils.rs:343:68
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Traceback (most recent call last):
  File "/.../polars_bug_example.py", line 13, in <module>
    df.select("email").collect(
  File "/.../lib/python3.9/site-packages/polars/utils.py", line 337, in wrapper
    return fn(*args, **kwargs)
  File "/.../lib/python3.9/site-packages/polars/internals/lazyframe/frame.py", line 1154, in collect
    return pli.wrap_df(ldf.collect())
pyo3_runtime.PanicException: called `Result::unwrap()` on an `Err` value: NotFound(Owned("users"))

Expected behavior

I expect to be able to select the email column from the users list of structs, after it has been exploded and unnested.

I found that there are two workarounds, but I'm not sure about the implications on performance. Hopefully this helps in finding the root cause:

  1. Turning off projection_pushdown optimization
  2. Creating a new column with an alias for the exploded column like:
    df = df.explode("users").with_column(pl.col("users").alias("users_flat"))
    df = df.unnest("users_flat")
    

Installed versions

---Version info---
Polars: 0.15.11
Index type: UInt32
Platform: macOS-12.6-arm64-arm-64bit
Python: 3.9.13 (main, Sep 29 2022, 15:54:06) 
[Clang 14.0.0 (clang-1400.0.29.102)]
---Optional dependencies---
pyarrow: 10.0.1
pandas: 1.5.2
numpy: 1.24.1
fsspec: <not installed>
connectorx: <not installed>
xlsx2csv: <not installed>
matplotlib: <not installed>
@martsec
Copy link

martsec commented Apr 9, 2024

Hi, I don't know if this fix is already in new polars versions, but I'm facing a similar issue in 1.76.0

The error given was Polars Failed: NoData(ErrString("empty container given"))

let raw: LazyFrame = JsonReader::new(reader: &mut cursor) // JsonReader<'_, &mut Cursor<…>>
    .finish() // Result<DataFrame, PolarsError>
    .unwrap() //DataFrame
    .lazy() //LazyFrame
    .explode(columns: ["aircraft"]) //LazyFrame
    .select(exprs: [col("now"), col("aircraft")]) //  I have to force this in order to work. doing col("*") did not work
    .unnest(cols: ["aircraft"]);

println!("{}", &raw.clone().collect()?);

I have a complex struct to unnest (58 fields), I don't know if this can affect a lot the projection.

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

Successfully merging a pull request may close this issue.

2 participants