Skip to content

Commit

Permalink
feat(rust, python): Support zero fill null strategy for binary and st…
Browse files Browse the repository at this point in the history
…ring columns (#13869)
  • Loading branch information
edavisau committed Jan 20, 2024
1 parent 7bea512 commit 625056b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/polars-core/src/chunked_array/ops/fill_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ fn fill_null_binary(ca: &BinaryChunked, strategy: FillNullStrategy) -> PolarsRes
FillNullStrategy::Max => {
ca.fill_null_with_values(ca.max_binary().ok_or_else(err_fill_null)?)
},
FillNullStrategy::Zero => ca.fill_null_with_values(&[]),
strat => polars_bail!(InvalidOperation: "fill-null strategy {:?} is not supported", strat),
}
}
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/functions/aggregation/test_horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ def test_sum_max_min() -> None:
assert_series_equal(out["min"], pl.Series("min", [1.0, 2.0, 3.0]))


def test_str_sum_horizontal() -> None:
df = pl.DataFrame(
{"A": ["a", "b", None, "c", None], "B": ["f", "g", "h", None, None]}
)
out = df.select(pl.sum_horizontal("A", "B"))
assert_series_equal(out["A"], pl.Series("A", ["af", "bg", "h", "c", ""]))


def test_cum_sum_horizontal() -> None:
df = pl.DataFrame(
{
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,10 +1025,16 @@ def test_fill_null() -> None:
b = pl.Series("b", ["a", None, "c", None, "e"])
assert b.fill_null(strategy="min").to_list() == ["a", "a", "c", "a", "e"]
assert b.fill_null(strategy="max").to_list() == ["a", "e", "c", "e", "e"]
assert b.fill_null(strategy="zero").to_list() == ["a", "", "c", "", "e"]
assert b.fill_null(strategy="forward").to_list() == ["a", "a", "c", "c", "e"]
assert b.fill_null(strategy="backward").to_list() == ["a", "c", "c", "e", "e"]

c = pl.Series("c", [b"a", None, b"c", None, b"e"])
assert c.fill_null(strategy="min").to_list() == [b"a", b"a", b"c", b"a", b"e"]
assert c.fill_null(strategy="max").to_list() == [b"a", b"e", b"c", b"e", b"e"]
assert c.fill_null(strategy="zero").to_list() == [b"a", b"", b"c", b"", b"e"]
assert c.fill_null(strategy="forward").to_list() == [b"a", b"a", b"c", b"c", b"e"]
assert c.fill_null(strategy="backward").to_list() == [b"a", b"c", b"c", b"e", b"e"]

df = pl.DataFrame(
[
Expand Down

0 comments on commit 625056b

Please sign in to comment.