dataframerustrust-polars

How to sum a column?


I have difficulty summing a column in Polars-Rust dataframe. E.g., the folowing snippet:

use polars::prelude::*;

fn main() {

    // let numbers = [1, 2, 3, 4, 5];
    let n = 5;
    let numbers: Vec<u32> = (1..=n).collect();
    let sum: u32 = numbers.iter().sum();
    println!("the sum is: {}", sum);

    let names: Vec<String> = (1..=n).map(|v| format!("row{v}")).collect();

    // creating dataframe
    let c1 = Column::new("names".into(), &names);
    let c2 = Column::new("numbers".into(), &numbers);
    let df = DataFrame::new(vec![c1, c2]).unwrap();
    println!("{:?}", df);

    let column_sum: u32 = df.column("numbers").iter().sum();
    println!("the columns sum is: {}", column_sum);

}

generates this error:

error[E0277]: a value of type `u32` cannot be made by summing an iterator over elements of type `&&polars::prelude::Column`
  --> src/sum_column.rs:19:55
   |
19 |     let column_sum: u32 = df.column("numbers").iter().sum();
   |                                                       ^^^ value of type `u32` cannot be made by summing a `std::iter::Iterator<Item=&&polars::prelude::Column>`
   |
   = help: the trait `std::iter::Sum<&&polars::prelude::Column>` is not implemented for `u32`
   = help: the following other types implement trait `std::iter::Sum<A>`:
             `u32` implements `std::iter::Sum<&u32>`
             `u32` implements `std::iter::Sum`
note: required by a bound in `std::iter::Iterator::sum`
  --> /rustc/9fc6b43126469e3858e2fe86cafb4f0fd5068869/library/core/src/iter/traits/iterator.rs:3515:5

For more information about this error, try `rustc --explain E0277`.
error: could not compile `sum_column` (bin "sum_column") due to 1 previous error

It seems from this message this is a minor issue... but I am new to Rust, and it is not clear what this message exactly suggest. Your help will be appreciated.


Solution

  • Quoting from docs:

    Series struct in rust-polars consists of typed ChunkArrays. To quickly cast a Series to a ChunkedArray you can call the method with the name of the type:

    let column_sum = df.column("numbers").unwrap().u32().unwrap().sum().unwrap()