0

How do I get a logical AND between polar-series to work ..

With scalar it does ?!

My understanding is that ".and" will yield a boolean mask of element-wise AND of two series.

use polars::prelude::*;

fn main() {
    let test_arr = [3, 5];
    let weekday = Series::new("weekday", test_arr);

    println!("{}", weekday);

    let a = weekday.gt_eq(0); // true / true
    let b = weekday.not_equal(5); // true / false
    let c = weekday.not_equal(4); // true / true

    // println!("{:?}{:?}{:?}", a, b, c);

    // so far everything works
    let a_b_comp = a.and(b); // ==> true / false
    println!("A/B test:\t{:?}", a_b_comp); // ==> true / false

    // after another AND ... it give me the wrong answer wy ?
    let a_b_c_comp = a_b_comp.and(c); // ==> true / true
    println!("A/B/C test:\t{:?}", a_b_c_comp); // ==> true / true

    let weekday = 5;

    let logical_test = (weekday >= 0) & (weekday != 5) & (weekday != 4);

    println!("w Scalars:\t{:?}", logical_test);
}


Robert
  • 131
  • 1
  • 7
  • `.get_eq()` and `.not_equal()` return a result, you are not calling the function you think your are calling, you are actually calling [`Result::and`](https://doc.rust-lang.org/std/result/enum.Result.html#method.and) – Bamontan Aug 19 '22 at 15:15
  • there is the [`.bitand()`](https://docs.rs/polars/0.23.2/polars/chunked_array/struct.ChunkedArray.html#impl-BitAnd%3C%26ChunkedArray%3CBooleanType%3E%3E-for-%26ChunkedArray%3CBooleanType%3E) method that exist on chunked array. you need to handle the result and then you can do the and operation you wanted on the array. – Bamontan Aug 19 '22 at 15:20
  • @Bamontan thanks for the hint ... this works ... I will post the working code below. – Robert Aug 19 '22 at 15:41

2 Answers2

1

Thanks to @Bamontan.

Here is the working code:

use polars::prelude::*;
use std::ops::BitAnd;

fn main() {
    let test_arr = [3, 5];
    let weekday = Series::new("weekday", test_arr);

    println!("{}", weekday);

    let a = weekday.gt_eq(0).unwrap(); // true / true
    let b = weekday.not_equal(5).unwrap(); // true / false
    let c = weekday.not_equal(4).unwrap(); // true / true

    // println!("{:?}{:?}{:?}", a, b, c);

    // so far everything works
    let a_b_comp = a.bitand(b); // ==> true / false
    println!("A/B test:\t{:?}", a_b_comp); // ==> true / false

    // working now with ".bitand"
    let a_b_c_comp = a_b_comp.bitand(c); // ==> true / false
    println!("A/B/C test:\t{:?}", a_b_c_comp); // ==> true / false

    let weekday = 5;

    let logical_test = (weekday >= 0) & (weekday != 5) & (weekday != 4);

    println!("w Scalars:\t{:?}", logical_test);
}

Robert
  • 131
  • 1
  • 7
0

This is the code with the last suggestion from @Bamontan:

use polars::prelude::*;

fn main() {
    let test_arr = [3, 5];
    let weekday = Series::new("weekday", test_arr);

    println!("{}", weekday);

    let a = weekday.gt_eq(0).unwrap(); // true / true
    let b = weekday.not_equal(5).unwrap(); // true / false
    let c = weekday.not_equal(4).unwrap(); // true / true

    // println!("{:?}{:?}{:?}", a, b, c);

    // so far everything works
    let a_b_comp = &a & &b; // ==> true / false
    println!("\nA/B test:\t{:?}", a_b_comp); // ==> true / false

    // working now with ".bitand"
    let a_b_c_comp = a & b & c; // ==> true / false
    println!("\nA/B/C test:\t{:?}", a_b_c_comp); // ==> true / false

    let weekday = 5;

    let logical_test = (weekday >= 0) & (weekday != 5) & (weekday != 4);

    println!("\nw Scalars:\t{:?}", logical_test);
}
Robert
  • 131
  • 1
  • 7