Polars example using list() function has compile error `can't find list`

I am trying to the last example on this page, but the compiler complains no method named listfound for enumExpr in the current scope. If I comment out the list() function look at what the compiler tells me: Err(ComputeError(ErrString("this operation is likely not what you want (you may need .list())\n\nError originated in expression: 'col(\"Type 1\").slice(offset=0, length=3).over([col(\"Type 1\")])'"))). The compiler is actually telling me I need to use the .list() function, but then when I do use it, the same compiler says it cannot find a list function. How can anyone learning a new language win against that kind of compiler? Here is my full code sample:

    let data: Vec<u8> = Client::new()
    .get("https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv")
    .send().unwrap()
    .text().unwrap()
    .bytes()
    .collect();

    let df = CsvReader::new(std::io::Cursor::new(data))
        .has_header(true)
        .finish();
    println!("pokemon: {:?}", df);

    let out = df.unwrap()
        .clone()
        .lazy()
        .select([
            col("Type 1")
                .head(Some(3))
                .list()
                .over(["Type 1"])
                .flatten(),
            col("Name")
                .sort_by(["Speed"], [false])
                .head(Some(3))
                .list()
                .over(["Type 1"])
                .flatten()
                .alias("fastest/group"),
            col("Name")
                .sort_by(["Attack"], [false])
                .head(Some(3))
                .list()
                .over(["Type 1"])
                .flatten()
                .alias("strongest/group"),
            col("Name")
                .sort(false)
                .head(Some(3))
                .list()
                .over(["Type 1"])
                .flatten()
                .alias("sorted_by_alphabet"),
        ])
        .collect();
    println!("flatten: {:?}", out);

This doesn't look like a compiler error report. It seems like a runtime error originating from polars.

You probably need to bring into scope the correct trait from polars.

I think you don't have the feature enabled that gates the Expr::list method: dtype-array. Try adding the dtype-array feature to your polars dependency. (not true)
The Expr::list method was added in the latest version v0.30.0 of polars, are you using the right version?

Yes, you are correct that it is a runtime error instead of a compiler error. My mistake.

That makes sense, as I am using version 0.29.0. When I try to use v0.30.0, I get the following compilation errors:

   Compiling polars-core v0.30.0
error[E0599]: no method named `is_sliced` found for reference `&Buffer<<S as PolarsNumericType>::Native>` in the current scope
  --> /Users/rogerbos/.cargo/registry/src/github.com-1ecc6299db9ec823/polars-core-0.30.0/src/chunked_array/ops/apply.rs:82:35
   |
82 |             if owned_arr.values().is_sliced() {
   |                                   ^^^^^^^^^ help: there is a method with a similar name: `slice`

error[E0599]: no method named `is_sliced` found for reference `&Buffer<<T as PolarsNumericType>::Native>` in the current scope
  --> /Users/rogerbos/.cargo/registry/src/github.com-1ecc6299db9ec823/polars-core-0.30.0/src/chunked_array/ops/extend.rs:65:25
   |
65 |         if arr.values().is_sliced() {
   |                         ^^^^^^^^^ help: there is a method with a similar name: `slice`

Those errors are in the polars apply.rs and extend.rs functions, so I don't know how to fix them.

Hmm strange. Could you please delete your Cargo.lock file and try again? This runs on my machine (though the flattening logic seems to be wrong):

Cargo.toml:

[dependencies]
polars = { version = "0.30.0", features = ["lazy"] }
reqwest = { version = "0.11.18", features = ["blocking"] }

main.rs:

use polars::prelude::*;
use reqwest::blocking::Client;

fn main() {
    let data: Vec<u8> = Client::new()
        .get("https://gist.githubusercontent.com/ritchie46/cac6b337ea52281aa23c049250a4ff03/raw/89a957ff3919d90e6ef2d34235e6bf22304f3366/pokemon.csv")
        .send()
        .unwrap()
        .text()
        .unwrap()
        .bytes()
        .collect();

    let df = CsvReader::new(std::io::Cursor::new(data))
        .has_header(true)
        .finish();

    println!("pokemon: {:?}", df);

    let out = df
        .unwrap()
        .clone()
        .lazy()
        .select([
            col("Type 1")
                .head(Some(3))
                .list()
                .0
                .over(["Type 1"])
                .flatten(),
            col("Name")
                .sort_by(["Speed"], [false])
                .head(Some(3))
                .list()
                .0
                .over(["Type 1"])
                .flatten()
                .alias("fastest/group"),
            col("Name")
                .sort_by(["Attack"], [false])
                .head(Some(3))
                .list()
                .0
                .over(["Type 1"])
                .flatten()
                .alias("strongest/group"),
            col("Name")
                .sort(false)
                .head(Some(3))
                .list()
                .0
                .over(["Type 1"])
                .flatten()
                .alias("sorted_by_alphabet"),
        ])
        .collect();

    println!("flatten: {:?}", out);
}

Note that I added .0 before every call to .over() to make it compile.

Thanks for taking the time to try it on your machine. I created a new project so I would have only the code you used and it compiled, but I got the following runtime error:

flatten: Err(ComputeError(ErrString("the length of the window expression did not match that of the group\n\nError originated in expression: 'col(\"Type 1\").slice(offset=0, length=3).over([col(\"Type 1\")])'")))

Yeah, me too.

Ok. At least you helped me get upgraded to version 0.30.0. Thanks again!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.