3

I would like to read the CSV file into a Polars dataframe.

Copying the code from the official documentation fails to run with cargo.

use polars::prelude::*;
use std::fs::File;

fn example() -> Result<DataFrame> {
    let file = File::open("iris.csv").expect("could not open file");

    CsvReader::new(file)
            .infer_schema(None)
            .has_header(true)
            .finish()
}

fn main() {
    let df = example();
    println!("{:?}", df);
}

results in

$ cargo run
   Compiling project v0.1.0
error[E0107]: this enum takes 2 generic arguments but 1 generic argument was supplied
 --> src/main.rs:4:17
  |
4 | fn example() -> Result<DataFrame> {
  |                 ^^^^^^ --------- supplied 1 generic argument
  |                 |
  |                 expected 2 generic arguments
  |
help: add missing generic argument
  |
4 | fn example() -> Result<DataFrame, E> {
  |                                 +++

For more information about this error, try `rustc --explain E0107`.
error: could not compile `eon-playground` due to previous error

3 Answers 3

3

You were reading the docs of an old version (you can see that by the "Go to latest version" at the top). In this version, polars::prelude exported an alias of Result. Now it is renamed to PolarsResult, and the updated example is:

use polars_core::prelude::*;
use polars_io::prelude::*;
use std::fs::File;

fn example() -> PolarsResult<DataFrame> {
    CsvReader::from_path("iris_csv")?
            .has_header(true)
            .finish()
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this! It worked, however, in addition, the following import was unused: use std::fs::File;.
2

I had further problems parsing the result (probably beginner problems, but sharing the solution in anyway). I was having compiler issues until I pre-initialised df as an empty DataFrame and then reassigned it in the match statement. This is probably not the only solution, but it gets you from CSV file to a DataFrame in Rust.


use polars::prelude::*;
// You should probably use polars_core rather

const TESTFILE: &str = "any.csv";
// Doesn't need to be a constant for you

fn read_csv(filename: &str) -> Result<DataFrame, PolarsError> {
    CsvReader::from_path(filename)?
        .has_header(true)
        .finish()
}

fn main() -> Result<(), PolarsError> {
    // * Import CSV
    let mut df = DataFrame::empty();
    match read_csv(TESTFILE) {
        Ok(x) => { df = x },
        Err(e) => { return Err(e) },
    };
     
    println!("{:?}", df.head(None));

    Ok(())
}

Comments

2

With the latest version of rust polars(0.44.2), the accepted answer does not work for me. It seems like the API's have now changed.

use polars::prelude::*;

fn main() {
    let df = CsvReadOptions::default()
        .with_has_header(true)
        .try_into_reader_with_file_path(Some("iris.csv".into()))
        .unwrap()
        .finish()
        .unwrap();
    println!("{:?}", df);
}

You also must have the polars-io feature enabled in Cargo.toml.

[dependencies]
polars = { version = "0.44.2", features = ["polars-io"] }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.