Search code examples
rustrust-polarslogical-and

How to get logical AND to work on polars series


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);
}



Solution

  • 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);
    }