Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rust, python): handle concat_list with first lit value #7235

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]]}
josh marked this conversation as resolved.
Show resolved Hide resolved

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