Skip to content

Commit

Permalink
fix(rust, python): fix Boolean::isin(null values) (#10074)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Jul 26, 2023
1 parent 700b965 commit e181a3e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 0 additions & 1 deletion polars/polars-core/src/chunked_array/ops/aggregate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ where

/// Booleans are casted to 1 or 0.
impl BooleanChunked {
/// Returns `None` if the array is empty or only contains null values.
pub fn sum(&self) -> Option<IdxSize> {
Some(if self.is_empty() {
0
Expand Down
8 changes: 7 additions & 1 deletion polars/polars-core/src/chunked_array/ops/is_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,13 @@ impl IsIn for BooleanChunked {
DataType::Boolean => {
let other = other.bool().unwrap();
let has_true = other.any();
let has_false = !other.all();
let nc = other.null_count();

let has_false = if nc == 0 {
!other.all()
} else {
!(other.sum().unwrap() as usize + nc) == other.len()
};
Ok(self.apply(|v| if v { has_true } else { has_false }))
}
_ => polars_bail!(opq = is_in, self.dtype(), other.dtype()),
Expand Down
6 changes: 3 additions & 3 deletions py-polars/tests/unit/operations/test_is_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def test_struct_logical_is_in() -> None:


def test_is_in_bool() -> None:
bool_value_to_filter_on = {True, None}
vals = [True, None]
df = pl.DataFrame({"A": [True, False, None]})
assert df.filter(pl.col("A").is_in(bool_value_to_filter_on)).to_dict(False) == {
"A": [True, False]
assert df.select(pl.col("A").is_in(vals)).to_dict(False) == {
"A": [True, False, None]
}


Expand Down

0 comments on commit e181a3e

Please sign in to comment.