Skip to content

Commit

Permalink
fix(rust, python): fix ternary with list output on empty frame (#5595)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Nov 23, 2022
1 parent 52487c2 commit 6d74407
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions polars/polars-lazy/src/physical_plan/expressions/ternary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ impl PhysicalExpr for TernaryExpr {
if falsy.is_empty() {
return Ok(falsy);
}
if mask.is_empty() {
return Ok(Series::new_empty(truthy.name(), truthy.dtype()));
}

expand_lengths(&mut truthy, &mut falsy, &mut mask);

Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ def test_streaming_empty_df() -> None:
assert df.lazy().join(df.lazy(), on="a", how="inner").filter(
2 == 1 # noqa: SIM300
).collect(allow_streaming=True).to_dict(False) == {"a": [], "b": [], "b_right": []}


def test_when_then_empty_list_5547() -> None:
out = pl.DataFrame({"a": []}).select([pl.when(pl.col("a") > 1).then([1])])
assert out.shape == (0, 1)
assert out.dtypes == [pl.List(pl.Int64)]

0 comments on commit 6d74407

Please sign in to comment.