Skip to content

Commit

Permalink
fix list concat (#3636)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jun 9, 2022
1 parent 82d6134 commit 32f7716
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
19 changes: 11 additions & 8 deletions polars/polars-ops/src/chunked_array/list/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,22 +300,25 @@ pub trait ListNameSpaceImpl: AsList {
continue;
}
};
let mut already_null = false;

let mut has_nulls = false;
for it in &mut iters {
match it.next().unwrap() {
Some(s) => {
acc.append(s.as_ref())?;
if !has_nulls {
acc.append(s.as_ref())?;
}
}
None => {
if !already_null {
builder.append_null();
already_null = true;
}

continue;
has_nulls = true;
}
}
}
if has_nulls {
builder.append_null();
continue;
}

match inner_type {
// structs don't have chunks, so we must first rechunk the underlying series
#[cfg(feature = "dtype-struct")]
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,17 @@ def test_list_ternary_concat() -> None:
"list2": [["789"], ["zzz"]],
"result": [["123", "456", "789"], ["zzz"]],
}


def test_list_concat_nulls() -> None:
assert pl.DataFrame(
{
"a": [["a", "b"], None, ["c", "d", "e"], None],
"t": [["x"], ["y"], None, None],
}
).with_column(pl.concat_list(["a", "t"]).alias("concat"))["concat"].to_list() == [
["a", "b", "x"],
None,
None,
None,
]

0 comments on commit 32f7716

Please sign in to comment.