From 6d74407e18c3738826b30a234ed1154fa339a8af Mon Sep 17 00:00:00 2001 From: Ritchie Vink Date: Wed, 23 Nov 2022 09:58:04 +0100 Subject: [PATCH] fix(rust, python): fix ternary with list output on empty frame (#5595) --- polars/polars-lazy/src/physical_plan/expressions/ternary.rs | 3 +++ py-polars/tests/unit/test_predicates.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/polars/polars-lazy/src/physical_plan/expressions/ternary.rs b/polars/polars-lazy/src/physical_plan/expressions/ternary.rs index 39c0ecc3f48b..dd380d96777e 100644 --- a/polars/polars-lazy/src/physical_plan/expressions/ternary.rs +++ b/polars/polars-lazy/src/physical_plan/expressions/ternary.rs @@ -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); diff --git a/py-polars/tests/unit/test_predicates.py b/py-polars/tests/unit/test_predicates.py index 4f765e74e45e..e0b2f703dbd8 100644 --- a/py-polars/tests/unit/test_predicates.py +++ b/py-polars/tests/unit/test_predicates.py @@ -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)]