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

fix(python): Fix nullable object in map_elements #20422

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions crates/polars-python/src/map/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,10 @@ where
S: FromPyObject<'py>,
{
let out = call_lambda(py, lambda, in_val)?;
match out.extract::<S>() {
Ok(s) => Ok(Some(s)),
Err(e) => {
if out.is_none() {
Ok(None)
} else {
Err(e)
}
},
if out.is_none() {
Ok(None)
} else {
out.extract::<S>().map(Some)
}
}

Expand Down
22 changes: 22 additions & 0 deletions py-polars/tests/unit/datatypes/test_object.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import io
from pathlib import Path
from uuid import uuid4
Expand Down Expand Up @@ -75,6 +77,26 @@ def test_nullable_object_13538() -> None:
}


def test_nullable_object_17936() -> None:
class Custom:
value: int

def __init__(self, value: int) -> None:
self.value = value

def mapper(value: int) -> Custom | None:
if value == 2:
return None
return Custom(value)

df = pl.DataFrame({"a": [1, 2, 3]})

assert df.select(
pl.col("a").map_elements(mapper, return_dtype=pl.Object).alias("with_dtype"),
pl.col("a").map_elements(mapper).alias("without_dtype"),
).null_count().row(0) == (1, 1)


def test_empty_sort() -> None:
df = pl.DataFrame(
data=[
Expand Down
Loading