Skip to content

Commit

Permalink
fix[python]: allow an apply that produces empty list values (#4437)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Aug 16, 2022
1 parent f1cd3c4 commit 4686f8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion py-polars/src/apply/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,16 @@ fn iterator_to_list(
}
builder.append_opt_series(first_value);
for opt_val in it {
builder.append_opt_series(opt_val.as_ref())
match opt_val {
None => builder.append_null(),
Some(s) => {
if s.len() == 0 && s.dtype() != dt {
builder.append_series(&Series::full_null("", 0, dt))
} else {
builder.append_series(&s)
}
}
}
}
Ok(builder.finish())
}
10 changes: 10 additions & 0 deletions py-polars/tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,13 @@ def test_apply_type_propagation() -> None:
]
)
).to_dict(False) == {"a": [1, 2, 3], "b": [1.0, 2.0, None]}


def test_empty_list_in_apply() -> None:
df = pl.DataFrame(
{"a": [[1], [1, 2], [3, 4], [5, 6]], "b": [[3], [1, 2], [1, 2], [4, 5]]}
)

assert df.select(
pl.struct(["a", "b"]).apply(lambda row: list(set(row["a"]) & set(row["b"])))
).to_dict(False) == {"a": [[], [1, 2], [], [5]]}

0 comments on commit 4686f8b

Please sign in to comment.