Skip to content

Commit

Permalink
fix: Wrong result on when().then().otherwise() on struct when both …
Browse files Browse the repository at this point in the history
…result are broadcast (#19000)

Co-authored-by: ritchie <[email protected]>
  • Loading branch information
barak1412 and ritchie46 authored Sep 29, 2024
1 parent ebd5302 commit 570e3a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/polars-core/src/chunked_array/ops/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ impl ChunkZip<StructType> for StructChunked {
let rechunked_validity = match (l.len(), r.len()) {
(1, 1) if length != 1 => match (l.null_count() == 0, r.null_count() == 0) {
(true, true) => None,
(true, false) => {
(false, true) => {
if mask.chunks().len() == 1 {
let m = mask.chunks()[0]
.as_any()
Expand All @@ -350,7 +350,7 @@ impl ChunkZip<StructType> for StructChunked {
)
}
},
(false, true) => {
(true, false) => {
if mask.chunks().len() == 1 {
let m = mask.chunks()[0]
.as_any()
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/functions/test_when_then.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,20 @@ def test_when_then_else_struct_18961() -> None:
)
assert expected == ans

df = pl.DataFrame({"left": v1, "right": v2, "mask": [True, False]})

expected2 = [None, {"foo": 0, "bar": "1"}]
ans = (
df.select(
pl.when(pl.col.mask)
.then(pl.col.left.first())
.otherwise(pl.col.right.first())
)
.get_column("left")
.to_list()
)
assert expected2 == ans


def test_when_then_supertype_15975() -> None:
df = pl.DataFrame({"a": [1, 2, 3]})
Expand Down

0 comments on commit 570e3a2

Please sign in to comment.