Robert
Robert

Reputation: 161

Rust Polars Series compare to scalar not working

I am trying to figure out, why the sample code does not work?

This is in my toml file:

polars = "*"

This is the sample from Polars Eager cookbook:

use polars::prelude::*;

fn main() {
    let s = Series::new("a", &[1, 2, 3]);
    let ca = UInt32Chunked::new("b", &[Some(3), None, Some(1)]);

    println!("{:?}", s.eq(2));

    println!("{:?}", ca.eq(2));
}

It looks like the "eq" function is not properly overloaded?! I am getting the following errors:

error[E0308]: mismatched types
   --> src\main.rs:7:27
    |
7   |     println!("{:?}", s.eq(2));
    |                        -- ^ expected `&polars::prelude::Series`, found integer
    |                        |
    |                        arguments to this function are incorrect
    |
note: associated function defined here
   --> C:\Users\rnio\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\cmp.rs:228:8
    |
228 |     fn eq(&self, other: &Rhs) -> bool;
    |        ^^


error[E0599]: `ChunkedArray<UInt32Type>` is not an iterator
   --> src\main.rs:9:25
    |
9   |     println!("{:?}", ca.eq(2));
    |                         ^^ `ChunkedArray<UInt32Type>` is not an iterator
    |
   ::: C:\Users\rnio\.cargo\registry\src\github.com-1ecc6299db9ec823\polars-core-0.22.7\src\chunked_array\mod.rs:143:1
    |
143 | pub struct ChunkedArray<T> {
    | -------------------------- doesn't satisfy `ChunkedArray<UInt32Type>: Iterator`
    |
    = note: the following trait bounds were not satisfied:
            `ChunkedArray<UInt32Type>: Iterator`
            which is required by `&mut ChunkedArray<UInt32Type>: Iterator`



Upvotes: 0

Views: 368

Answers (1)

Robert
Robert

Reputation: 161

Thanks to @isaactfa ... the current workaround is to convert the Series to a ChunckedArray before comparisons.

Here is a working code:

use polars::prelude::*;

fn main() {
    let s = Series::new("a", &[1, 2, 3]);
    let ca = UInt32Chunked::new("b", &[Some(3), None, Some(1)]);

    println!("{:?}", s.i32().unwrap().equal(2));

    println!("{:?}", ca.equal(3));
}

Upvotes: 1

Related Questions