0

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`



Robert
  • 131
  • 1
  • 7
  • The docs might be outdated, those operators seem to be implemented in the [`ChunkCompare` trait](https://pola-rs.github.io/polars/polars/series/trait.ChunkCompare.html) where `eq` and `neq` are called `equal` and `not_equal`. – isaactfa Aug 21 '22 at 14:16
  • Thanks that helped ... so I convert the SERIES to a ChunkedArray and then apply the comparisons ... – Robert Aug 22 '22 at 08:10

1 Answers1

1

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

Robert
  • 131
  • 1
  • 7
  • The `ChunkCompare` trait is actually implemented for `Series`, there's no need to cast it to a `ChunkedArray` first. You can just call `s.equal(2)`. – isaactfa Aug 22 '22 at 09:19
  • When is lit(1) required? – Edmund's Echo Apr 01 '23 at 16:20
  • In the polars library, the lit function is used to create a literal value that can be used in expressions. It is usually required when you want to perform operations on a DataFrame or Series using constant values along with columns. For example, if you want to add a constant value to a column in a DataFrame or perform arithmetic operations using a constant, you would use lit. The lit function is particularly useful when constructing expressions in polars. – Robert Apr 02 '23 at 17:54