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

Bug Report - SchemaError with apply Function in Polars 0.20.3 #14723

Closed
1 task done
maazizzahra opened this issue Feb 27, 2024 · 3 comments
Closed
1 task done

Bug Report - SchemaError with apply Function in Polars 0.20.3 #14723

maazizzahra opened this issue Feb 27, 2024 · 3 comments
Labels
bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@maazizzahra
Copy link

maazizzahra commented Feb 27, 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

lf = pl.LazyFrame({'a': ['[]']})
lf.select(pl.col('a').apply(eval, return_dtype=pl.List(pl.Utf8))).collect()
Polars + moderne :
lf.select(pl.col('a').map_elements(eval, return_dtype=pl.List(pl.String))).collect()
-> crash

I encountered the following error: polars.exceptions.SchemaError: expected output type 'List(String)', got 'List(Null)'; set return_dtype to the proper datatype
To resolve the issue, I used the following workaround:

lf.select(pl.when(pl.col('a') == '[]').then(pl.lit([]).cast(pl.List(pl.String))).otherwise(pl.col('a').map_elements(lambda x: eval(x) or [''], return_dtype=pl.List(pl.String)))).collect()

we use  Polars 0.20.3 

Installed versions

I use 0.20.3```not 0.20.1

</details>
@maazizzahra maazizzahra added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Feb 27, 2024
@MarcoGorelli
Copy link
Collaborator

thanks for the report, could you show an example of input data please

@MarcoGorelli MarcoGorelli added needs triage Awaiting prioritization by a maintainer and removed needs triage Awaiting prioritization by a maintainer labels Feb 27, 2024
@maazizzahra maazizzahra changed the title Bug Report - SchemaError with apply Function in Polars 0.20.1 Bug Report - SchemaError with apply Function in Polars 0.20.3 Feb 29, 2024
@cmdlineluser
Copy link
Contributor

cmdlineluser commented Feb 29, 2024

Is this the same issue that is discussed in #10398 (comment)?

i.e. List(null) in this case should satisfy/upcast to List(String)

You can also force the dtype by manually wrapping in a Series inside the callback.

lf.select(pl.col('a').map_elements(eval)).collect()
# shape: (1, 1)
# ┌────────────┐
# │ a          │
# │ ---        │
# │ list[null] │ # <- null
# ╞════════════╡
# │ []         │
# └────────────┘

lf.select(
    pl.col('a').map_elements(lambda x: pl.Series(eval(x), dtype=pl.String))
).collect()
# shape: (1, 1)
# ┌───────────┐
# │ a         │
# │ ---       │
# │ list[str] │ # <- str
# ╞═══════════╡
# │ []        │
# └───────────┘

@stinodego
Copy link
Member

Should be fixed by #15699

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

4 participants