-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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: Ensure ignore_nulls
is respected in horizontal sum/mean
#20469
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -556,3 +556,68 @@ def test_horizontal_sum_boolean_with_null() -> None: | |
) | ||
|
||
assert_frame_equal(out.collect(), expected_df) | ||
|
||
|
||
@pytest.mark.parametrize("ignore_nulls", [True, False]) | ||
@pytest.mark.parametrize( | ||
("dtype_in", "dtype_out"), | ||
[ | ||
(pl.Null, pl.Null), | ||
(pl.Boolean, pl.UInt32), | ||
(pl.UInt8, pl.UInt8), | ||
(pl.Float32, pl.Float32), | ||
(pl.Float64, pl.Float64), | ||
(pl.Decimal(None, 5), pl.Decimal(None, 5)), | ||
], | ||
) | ||
def test_horizontal_sum_with_null_col_ignore_strategy( | ||
dtype_in: PolarsDataType, | ||
dtype_out: PolarsDataType, | ||
ignore_nulls: bool, | ||
) -> None: | ||
lf = pl.LazyFrame( | ||
{ | ||
"null": [None, None, None], | ||
"s": pl.Series([1, 0, 1], dtype=dtype_in, strict=False), | ||
"s2": pl.Series([1, 0, None], dtype=dtype_in, strict=False), | ||
} | ||
) | ||
result = lf.select(pl.sum_horizontal("null", "s", "s2", ignore_nulls=ignore_nulls)) | ||
if ignore_nulls and dtype_in != pl.Null: | ||
values = [2, 0, 1] | ||
else: | ||
values = [None, None, None] # type: ignore[list-item] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong. If our sum doesn't ignore nulls, it doesn't propagate them, but replaces them with the identity: 0. The horizontal semantics should be the same as the vertical semantics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you have it reversed.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean that our There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what you're saying is that the Given that the current implementation has the parameter, and that there is an issue with it ( This PR contains a small fix for the float32 case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think you're right. Consider it an observation. ;) Will take a look a bit later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Ritchie. I have a follow-up to this adding temporals for mean horizontal but I'll wait for this one first. |
||
expected = pl.LazyFrame(pl.Series("null", values, dtype=dtype_out)) | ||
assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("ignore_nulls", [True, False]) | ||
@pytest.mark.parametrize( | ||
("dtype_in", "dtype_out"), | ||
[ | ||
(pl.Null, pl.Float64), | ||
(pl.Boolean, pl.Float64), | ||
(pl.UInt8, pl.Float64), | ||
(pl.Float32, pl.Float32), | ||
(pl.Float64, pl.Float64), | ||
], | ||
) | ||
def test_horizontal_mean_with_null_col_ignore_strategy( | ||
dtype_in: PolarsDataType, | ||
dtype_out: PolarsDataType, | ||
ignore_nulls: bool, | ||
) -> None: | ||
lf = pl.LazyFrame( | ||
{ | ||
"null": [None, None, None], | ||
"s": pl.Series([1, 0, 1], dtype=dtype_in, strict=False), | ||
"s2": pl.Series([1, 0, None], dtype=dtype_in, strict=False), | ||
} | ||
) | ||
result = lf.select(pl.mean_horizontal("null", "s", "s2", ignore_nulls=ignore_nulls)) | ||
if ignore_nulls and dtype_in != pl.Null: | ||
values = [1, 0, 1] | ||
else: | ||
values = [None, None, None] # type: ignore[list-item] | ||
expected = pl.LazyFrame(pl.Series("null", values, dtype=dtype_out)) | ||
assert_frame_equal(result, expected) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't have to be in this PR, but this should actually be IndexType.