Getting started with polars

I am learning how to use Polars, but I am having to modify all of the example code to get it to run. For example the docs show this:

let out = df
        col("names").first().alias("first name"),
        (mean("nrs") * lit(10)).alias("10xnrs"),
println!("{}", out);

I have to modify it 3 ways to make it complie. 1) add .expect("REASON"), 2) remove the "?", and 3) use "{:?}" instead of just "{}". For example:

    let out = df.expect("define out")
        col("names").first().alias("first name"),
        (mean("nrs") * lit(10)).alias("10xnrs"),
    println!("show off context: {:?}", out);

This is just one example, I have to make these changes to every example. I admit I am new to Rust, so my question for any Polars experts, is there something wrong with my Rust setup, or are the docs just outdated for the latest version of rust? If the latter, is Polars still in active development? Is it worthwhile to learn, or should I focus on native rust ways to manipulate data? TIA.

The key is error handling:

The doc uses ? to propagate errors, but your modification just uses unwrap/expect to panic on error.

1 Like

Thanks for the answer. It makes sense and in retrospect I guess I knew that, so maybe I asked the wrong question. I should have asked why does my compiler make me change the code and refuse to run the doc example as is. I didn't add the .expect() of my own volition, the code would not compile without that change. And of course when I made that change I also had to remove the "?". My second question, why does println!("{}") work for the doc author, but I have to use "{:?}"? Here are my dependencies, I think they are all the latest version:

chrono = "0.4.24"
connectorx = {features = ["src_postgres", "dst_arrow2"]}
polars-sql = "0.29.0"
polars = { version = "0.29.0", features = ["lazy","parquet"]}

The dependencies don't really matter. You can only use the ? operator in functions that return a Result [1]. If you don't really care about the error, you can use the following signature:

fn func() -> Result<(), Box<dyn Error>>

where Error is std::error::Error. If you want to return a useable value, replace () by it. The compiler should probably have told you that though?

  1. Or Option, or any other type that implements FromResidual. ↩︎

This is part of a more general problem of wanting to supply short examples that focus on the functionality be documented, but then ending up with an example that doesn't run without a lot of elided boilerplate. mdbook examples let you hide the boilerplate but still provide a runnable example, and have a button to let the user see the boilerplate. But the polars website is using something else. [1]

Without any error output this is somewhat of an educated guess, but I think

  • Whatever boilerplate of your own that you provided resulted in df being a Result or something, which is why you needed df.expect() whereas they didn't use df?

  • When you removed ? on the collect() you changed the type of out from DataFrame [2] to Result<DataFrame, _> and that's why you need {:?}

    • So put .expect() there too (or better, make your boilerplate work with ?)

  1. And is just generally a very annoying site, to me anyway. ↩︎

  2. at a guess ↩︎

Due to the type of out that the following code is based on. Accroding to collect signature

pub fn collect(self) -> Result<DataFrame, PolarsError>
  • the type in doc is DataFrame[1]
  • but the type in your code is Result<DataFrame, PolarsError> when you haven't handle the error via ? or expect

println! uses the out and calls methods from out, the question becomes which one is used here.

According to formatting-traits, {} will call fmt method in Display in std::fmt - Rust and {:?} will call fmt method in Debug in std::fmt - Rust .

out in doc implements both Debug and Display, so it compiles as expected.

But in your code the type Result only implements Debug if its generics implement Debug. There is no fmt method from Display to be called, thus your code won't compile if {} and compiles if {:?}.

  1. since ? removes the outer Result ↩︎

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.