From 6f42103351699d5838fdc30fb0f22e8d86d59c80 Mon Sep 17 00:00:00 2001 From: Edward Davis Date: Sat, 20 Jan 2024 14:56:18 +1000 Subject: [PATCH 1/4] implement fill null zero for binary dtype --- crates/polars-core/src/chunked_array/ops/fill_null.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/polars-core/src/chunked_array/ops/fill_null.rs b/crates/polars-core/src/chunked_array/ops/fill_null.rs index efab235944e0..62a1229b7019 100644 --- a/crates/polars-core/src/chunked_array/ops/fill_null.rs +++ b/crates/polars-core/src/chunked_array/ops/fill_null.rs @@ -383,6 +383,9 @@ 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), } } From 5d0bd2d28b673e0a4ecd27346ae1abff7797b9a0 Mon Sep 17 00:00:00 2001 From: Edward Davis Date: Sat, 20 Jan 2024 15:09:27 +1000 Subject: [PATCH 2/4] add fill null tests --- py-polars/tests/unit/series/test_series.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/py-polars/tests/unit/series/test_series.py b/py-polars/tests/unit/series/test_series.py index 58712f532afa..73b11b203005 100644 --- a/py-polars/tests/unit/series/test_series.py +++ b/py-polars/tests/unit/series/test_series.py @@ -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( [ From b21b3d89344ee05062d4614c8901127ca22417ee Mon Sep 17 00:00:00 2001 From: Edward Davis Date: Sat, 20 Jan 2024 15:16:10 +1000 Subject: [PATCH 3/4] add horizontal sum test for strings --- .../tests/unit/functions/aggregation/test_horizontal.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/py-polars/tests/unit/functions/aggregation/test_horizontal.py b/py-polars/tests/unit/functions/aggregation/test_horizontal.py index 1955edc32335..430f52e12be8 100644 --- a/py-polars/tests/unit/functions/aggregation/test_horizontal.py +++ b/py-polars/tests/unit/functions/aggregation/test_horizontal.py @@ -240,6 +240,12 @@ 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( { From 1e8765692400247afd9c6af178c1db5ddbb20e2f Mon Sep 17 00:00:00 2001 From: Edward Davis Date: Sat, 20 Jan 2024 15:20:13 +1000 Subject: [PATCH 4/4] fmt --- crates/polars-core/src/chunked_array/ops/fill_null.rs | 4 +--- py-polars/tests/unit/functions/aggregation/test_horizontal.py | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/polars-core/src/chunked_array/ops/fill_null.rs b/crates/polars-core/src/chunked_array/ops/fill_null.rs index 62a1229b7019..9458021cf92d 100644 --- a/crates/polars-core/src/chunked_array/ops/fill_null.rs +++ b/crates/polars-core/src/chunked_array/ops/fill_null.rs @@ -383,9 +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(&[]) - }, + FillNullStrategy::Zero => ca.fill_null_with_values(&[]), strat => polars_bail!(InvalidOperation: "fill-null strategy {:?} is not supported", strat), } } diff --git a/py-polars/tests/unit/functions/aggregation/test_horizontal.py b/py-polars/tests/unit/functions/aggregation/test_horizontal.py index 430f52e12be8..7796046d1d3d 100644 --- a/py-polars/tests/unit/functions/aggregation/test_horizontal.py +++ b/py-polars/tests/unit/functions/aggregation/test_horizontal.py @@ -241,7 +241,9 @@ def test_sum_max_min() -> None: def test_str_sum_horizontal() -> None: - df = pl.DataFrame({"A": ["a", "b", None, "c", None], "B": ["f", "g", "h", None, 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", ""]))