Skip to content

Commit

Permalink
fix(rust, python): handle concat_list with first lit value (#7235)
Browse files Browse the repository at this point in the history
  • Loading branch information
josh authored Feb 28, 2023
1 parent 86f8be0 commit a7cd77f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions polars/polars-lazy/polars-plan/src/dsl/function_expr/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,22 @@ pub(super) fn concat(s: &mut [Series]) -> PolarsResult<Option<Series>> {
let mut first = std::mem::take(&mut s[0]);
let other = &s[1..];

let first_ca = match first.list().ok() {
let mut first_ca = match first.list().ok() {
Some(ca) => ca,
None => {
first = first.reshape(&[-1, 1]).unwrap();
first.list().unwrap()
}
};
}
.clone();

if first_ca.len() == 1 && !other.is_empty() {
let max_len = other.iter().map(|s| s.len()).max().unwrap();
if max_len > 1 {
first_ca = first_ca.new_from_index(0, max_len)
}
}

first_ca.lst_concat(other).map(|ca| Some(ca.into_series()))
}

Expand Down
12 changes: 12 additions & 0 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ def test_flat_aggregation_to_list_conversion_6918() -> None:
).to_dict(False) == {"a": [1, 2], "b": [[[0.0, 1.0]], [[3.0, 4.0]]]}


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

assert df.select(pl.concat_list([pl.col("a"), pl.lit(1)]).alias("a")).to_dict(
False
) == {"a": [[1, 1], [2, 1], [3, 1]]}

assert df.select(pl.concat_list([pl.lit(1), pl.col("a")]).alias("a")).to_dict(
False
) == {"a": [[1, 1], [1, 2], [1, 3]]}


def test_list_count_match() -> None:
assert pl.DataFrame({"listcol": [[], [1], [1, 2, 3, 2], [1, 2, 1], [4, 4]]}).select(
pl.col("listcol").arr.count_match(2).alias("number_of_twos")
Expand Down

0 comments on commit a7cd77f

Please sign in to comment.