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
.clone()
.lazy()
.select([
sum("nrs"),
col("names").sort(false),
col("names").first().alias("first name"),
(mean("nrs") * lit(10)).alias("10xnrs"),
])
.collect()?;
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")
.clone()
.lazy()
.select([
sum("nrs"),
col("names").sort(false),
col("names").first().alias("first name"),
(mean("nrs") * lit(10)).alias("10xnrs"),
])
.collect();
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.
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:
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?
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 ?)
And is just generally a very annoying site, to me anyway. ↩︎
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 {:?}.