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(python,rust): str.concat on empty list #12066

Merged
merged 3 commits into from
Oct 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
6 changes: 5 additions & 1 deletion crates/polars-ops/src/chunked_array/strings/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use polars_core::prelude::*;

// Vertically concatenate all strings in a Utf8Chunked.
pub fn str_concat(ca: &Utf8Chunked, delimiter: &str) -> Utf8Chunked {
if ca.len() <= 1 {
if ca.is_empty() {
Julian-J-S marked this conversation as resolved.
Show resolved Hide resolved
return Utf8Chunked::new(ca.name(), &[""]);
}

if ca.len() == 1 {
return ca.clone();
}

Expand Down
22 changes: 22 additions & 0 deletions py-polars/tests/unit/namespaces/string/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ def test_str_concat2() -> None:
assert cast(str, df.item()) == "1-null-2"


def test_str_concat_empty_list() -> None:
s = pl.Series([], dtype=pl.Utf8)
result = s.str.concat()
expected = pl.Series([""])
assert_series_equal(result, expected)


def test_str_concat_empty_list2() -> None:
s = pl.Series([], dtype=pl.Utf8)
df = pl.DataFrame({"foo": s})
result = df.select(pl.col("foo").str.concat()).item()
expected = ""
assert result == expected


def test_str_concat_empty_list_agg_context() -> None:
df = pl.DataFrame(data={"i": [1], "v": [None]}, schema_overrides={"v": pl.Utf8})
result = df.group_by("i").agg(pl.col("v").drop_nulls().str.concat())["v"].item()
expected = ""
assert result == expected


def test_str_concat_datetime() -> None:
df = pl.DataFrame({"d": [datetime(2020, 1, 1), None, datetime(2022, 1, 1)]})
df = df.select(pl.col("d").str.concat("|"))
Expand Down