Skip to content

Commit

Permalink
fix(python,rust): str.concat on empty list (#12066)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian-J-S authored Oct 28, 2023
1 parent 440e093 commit 3f7c190
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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() {
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

0 comments on commit 3f7c190

Please sign in to comment.