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

map_elements silently swallows Exceptions when return_dtype= is set #19315

Closed
2 tasks done
cmdlineluser opened this issue Oct 19, 2024 · 2 comments · Fixed by #20417
Closed
2 tasks done

map_elements silently swallows Exceptions when return_dtype= is set #19315

cmdlineluser opened this issue Oct 19, 2024 · 2 comments · Fixed by #20417
Assignees
Labels
accepted Ready for implementation bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars

Comments

@cmdlineluser
Copy link
Contributor

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

pl.select(
    pl.lit(1).map_elements(lambda x: 
        I | DO | NOT | EXIST,
        return_dtype=pl.String
    )
)

# shape: (1, 1)
# ┌─────────┐
# │ literal │
# │ ---     │
# │ str     │
# ╞═════════╡
# │ null    │
# └─────────┘

Log output

No response

Issue description

I previously encountered this while debugging a related issue: #14821 (comment) (some notes there)

Breaking out as a separate issue as I just ran into a confusing few minutes due to it.

Expected behavior

.map_batches propagates the error.

# ComputeError: NameError: name 'I' is not defined

Installed versions

--------Version info---------
Polars:               1.9.0
Index type:           UInt32
Platform:             macOS-13.6.1-arm64-arm-64bit
Python:               3.12.2 (main, Feb  6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)]

----Optional dependencies----
adbc_driver_manager:  <not installed>
cloudpickle:          <not installed>
connectorx:           <not installed>
deltalake:            <not installed>
fastexcel:            <not installed>
fsspec:               <not installed>
gevent:               <not installed>
great_tables:         <not installed>
hvplot:               <not installed>
matplotlib:           <not installed>
nest_asyncio:         <not installed>
numpy:                1.26.4
openpyxl:             <not installed>
pandas:               2.2.1
pyarrow:              15.0.2
pydantic:             <not installed>
pyiceberg:            <not installed>
sqlalchemy:           <not installed>
torch:                <not installed>
xlsx2csv:             <not installed>
xlsxwriter:           <not installed>
@cmdlineluser cmdlineluser added bug Something isn't working needs triage Awaiting prioritization by a maintainer python Related to Python Polars labels Oct 19, 2024
@DavideCanton
Copy link

I was going to open a separate issue, but maybe it's better to add what I noticed to this, since the source of the problem seems the same.

This behavior is quite unfortunate when it comes to handling warning filters.

This code:

import warnings
import polars as pl


def warns(x):
    warnings.warn("deprecated", DeprecationWarning)
    return x


df = pl.DataFrame({"a": [1, 2, 3, 4, 5]})
print(df.select(pl.col("a").map_elements(warns, return_dtype=pl.Int64)))

If ran with python foo.py prints:

/home/canton/source/prove/foo.py:6: DeprecationWarning: deprecated
  warnings.warn("deprecated", DeprecationWarning)
shape: (5, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 2   │
│ 3   │
│ 4   │
│ 5   │
└─────┘

while if ran with python -W error foo.py prints:

shape: (5, 1)
┌──────┐
│ a    │
│ ---  │
│ i64  │
╞══════╡
│ null │
│ null │
│ null │
│ null │
│ null │
└──────┘

Removing the return_dtype parameter restores the correct behavior.

@maitreyakv
Copy link

I have noticed that adding in the skip_nulls=True in map_elements option seems to resolve the behavior, and the exception is properly raised.

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
Development

Successfully merging a pull request may close this issue.

5 participants