Skip to content

Commit

Permalink
fix when-then-chain (#3048)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 3, 2022
1 parent bc6d4b6 commit 31e8b30
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion polars/polars-core/src/chunked_array/ops/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn prepare_mask(mask: &BooleanArray) -> BooleanArray {

match mask.validity() {
// nulls are set to true meaning we take from the right in the zip/ if_then_else kernel
Some(validity) if validity.null_count() == 0 => {
Some(validity) if validity.null_count() != 0 => {
let mask = mask.values() & validity;
BooleanArray::from_data_default(mask, None)
}
Expand Down
35 changes: 35 additions & 0 deletions polars/tests/it/lazy/expressions/arity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,40 @@ fn includes_null_predicate_3038() -> Result<()> {
"b" => [Some("good hit"), None, None],
}?;
assert!(res.frame_equal_missing(&exp_df));

let df = df! {
"a" => ["a1", "a2", "a3", "a4", "a2"],
"b" => [Some("tree"), None, None, None, None],
}?;
let res = df
.lazy()
.with_column(
when(col("b").map(
move |s| {
s.utf8()?
.to_lowercase()
.contains("non-existent")
.map(Into::into)
},
GetOutput::from_type(DataType::Boolean),
))
.then(lit("weird-1"))
.when(col("a").eq(lit("a1".to_string())))
.then(lit("ok1"))
.when(col("a").eq(lit("a2".to_string())))
.then(lit("ok2"))
.when(lit(true))
.then(lit("ft"))
.otherwise(Expr::Literal(LiteralValue::Null))
.alias("c"),
)
.collect()?;
let exp_df = df! {
"a" => ["a1", "a2", "a3", "a4", "a2"],
"b" => [Some("tree"), None, None, None, None],
"c" => ["ok1", "ok2", "ft", "ft", "ok2"]
}?;
assert!(res.frame_equal_missing(&exp_df));

Ok(())
}

0 comments on commit 31e8b30

Please sign in to comment.