English 中文(简体)
Reading file with polars and ignoring errors
原标题:

I would like to read in a CSV file in polars using rust, and I would like to ignore all rows that cause an error. My understanding was that using with_ignore_errors(true) would skip the rows that cause error. However, I get an error.

use polars::frame::DataFrame;
use polars::prelude::PolarsResult;
use polars_io::prelude::*;

fn open_file(file_path: &str) -> PolarsResult<DataFrame> {
    CsvReader::from_path(file_path)?
        .with_ignore_errors(true)
        .has_header(true)
        .finish()
}


pub fn calculate(file_path: &str, lhs: &Vec<usize>, rhs: usize) -> f64 {
    let df = match open_file(file_path) {
        Ok(df) => df,
        Err(_) => panic!("Panic"),
    };
    return calculate_all(&df, &lhs, rhs);
}

Here is the error:

thread  <unnamed>  panicked at  called `Result::unwrap()` on an `Err` value: External("", Utf8Error) , /home/user/.cargo/registry/src/index.crates.io
-6f17d22bba15001f/arrow2-0.17.3/src/array/growable/utf8.rs:55:76
stack backtrace:
   0:     0x55de9f9eaf01 - std::backtrace_rs::backtrace::libunwind::trace::h6aeaf83abc038fe6
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x55de9f9eaf01 - std::backtrace_rs::backtrace::trace_unsynchronized::h4f9875212db0ad97
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x55de9f9eaf01 - std::sys_common::backtrace::_print_fmt::h3f820027e9c39d3b
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x55de9f9eaf01 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hded4932df41373b3
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x55de9fa0f3bf - core::fmt::rt::Argument::fmt::hc8ead7746b2406d6
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/fmt/rt.rs:138:9
   5:     0x55de9fa0f3bf - core::fmt::write::hb1cb56105a082ad9
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/core/src/fmt/mod.rs:1094:21
   6:     0x55de9f9e8ac1 - std::io::Write::write_fmt::h797fda7085c97e57
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/io/mod.rs:1713:15
   7:     0x55de9f9ead15 - std::sys_common::backtrace::_print::h492d3c92d7400346
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x55de9f9ead15 - std::sys_common::backtrace::print::hf74aa2eef05af215
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x55de9f9ec367 - std::panicking::default_hook::{{closure}}::h8cad394227ea3de8
  10:     0x55de9f9ec154 - std::panicking::default_hook::h249cc184fec99a8a
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:288:9
  11:     0x55de9f9ec81c - std::panicking::rust_panic_with_hook::h82ebcd5d5ed2fad4
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:705:13
  12:     0x55de9f9ec717 - std::panicking::begin_panic_handler::{{closure}}::h810bed8ecbe66f1a
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:597:13
  13:     0x55de9f9eb336 - std::sys_common::backtrace::__rust_end_short_backtrace::h1410008071796261
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/sys_common/backtrace.rs:151:18
  14:     0x55de9f9ec462 - rust_begin_unwind
                               at /rustc/8ede3aae28fe6e4d52b38157d7bfe0d3bceef225/library/std/src/panicking.rs:593:5

My understanding is that using with_ignore_errors should skip the rows that have error. I also expected any error from the CsvReader to return to calculate -- I expected "Panic" to print since open_file(...) would return the error from CsvReader::from_path

问题回答

暂无回答




相关问题
Creating an alias for a variable

I have the following code in Rust (which will not compile but illustrates what I am after). For readability purposes, I would like to refer the same string with two different names so that the name of ...

Rust Visual Studio Code code completion not working

I m trying to learn Rust and installed the Rust extension for VSCode. But I m not seeing auto-completions for any syntax. I d like to call .trim() on String but I get no completion for it. I read that ...

热门标签