Nulls imputation with Rust Polars dataframe takes longer than Pandas in Python

I benchmark the imputation of null values with zeroes in a large dataframe saved as parquet file. The steps of the test are:

  1. Read a ~800Mb parquet file which stores a dataframe with 10000×10000 dimension.
  2. Replace any null values with zeroes
  3. Save the filled dataframe into a parquet file

I am doing the same test in Python/Pandas and Rust/Polars. I was hoping that Polars in Rust will be much faster than Pandas in Python but the total load/process/store duration in Rust is ~20 seconds while in Pandas takes only ~16 seconds. Parallelization is not used by either solution, as I can see but still I was expecting that Rust should be much faster in this case. Anything that I can do to maximize the processing speed in Rust?

The code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Some(Commands::Impute { input_path, output_path }) => {
// Start measuring time
let start = Instant::now();
let df = polars2::read_parquet(&input_path);
let mut imputed_df = polars2::fill_nan_with_zeroes(&df).expect("Error");
polars2::write_parquet(&mut imputed_df, &output_path);
// End measuring time
let duration = start.elapsed();
println!("Total duration: {:.2} seconds", duration.as_secs_f64());
}
// utilities for working with polars dataframes
//
use polars::prelude::*;
use std::fs::File;
//use polars::df;
//read in a parquet file
pub fn read_parquet(path: &str) -> DataFrame {
// Open file
let file = File::open(path).expect("Failed to open file");
// Read to DataFrame and Return
ParquetReader::new(file)
.finish()
.expect("Failed to read Parquet file")
}
// write dataframe to parquet file
pub fn write_parquet(df: &mut DataFrame, path: &str) {
// create a file
let file = File::create(path)
.expect("could not create output file");
// write dataframe to output parquet
ParquetWriter::new(file)
.finish(df)
.expect("Failed to write dataframe.");
}
//print "n" rows of a dataframe
pub fn print_df(df: &DataFrame, n: usize) {
println!("{:?}", df.head(Some(n)));
}
//print the schema of a dataframe
pub fn print_schema(df: &DataFrame) {
println!("{:?}", df.schema());
}
//print the shape of a dataframe
pub fn print_shape(df: &DataFrame) {
println!("{:?}", df.shape());
}
/// Replaces NaN with zeroes.
pub fn fill_nan_with_zeroes(df: &DataFrame) -> PolarsResult<DataFrame> {
let mut transformed_df = df.clone();
for idx in 0..transformed_df.width() {
transformed_df.try_apply_at_idx(idx, |series| {
if let Ok(ca) = series.f64() {
let ca_filled = ca.fill_null_with_values(0.0);
ca_filled
} else {
series.f64().cloned()
}
})?;
}
Ok(transformed_df)
}
</code>
<code>Some(Commands::Impute { input_path, output_path }) => { // Start measuring time let start = Instant::now(); let df = polars2::read_parquet(&input_path); let mut imputed_df = polars2::fill_nan_with_zeroes(&df).expect("Error"); polars2::write_parquet(&mut imputed_df, &output_path); // End measuring time let duration = start.elapsed(); println!("Total duration: {:.2} seconds", duration.as_secs_f64()); } // utilities for working with polars dataframes // use polars::prelude::*; use std::fs::File; //use polars::df; //read in a parquet file pub fn read_parquet(path: &str) -> DataFrame { // Open file let file = File::open(path).expect("Failed to open file"); // Read to DataFrame and Return ParquetReader::new(file) .finish() .expect("Failed to read Parquet file") } // write dataframe to parquet file pub fn write_parquet(df: &mut DataFrame, path: &str) { // create a file let file = File::create(path) .expect("could not create output file"); // write dataframe to output parquet ParquetWriter::new(file) .finish(df) .expect("Failed to write dataframe."); } //print "n" rows of a dataframe pub fn print_df(df: &DataFrame, n: usize) { println!("{:?}", df.head(Some(n))); } //print the schema of a dataframe pub fn print_schema(df: &DataFrame) { println!("{:?}", df.schema()); } //print the shape of a dataframe pub fn print_shape(df: &DataFrame) { println!("{:?}", df.shape()); } /// Replaces NaN with zeroes. pub fn fill_nan_with_zeroes(df: &DataFrame) -> PolarsResult<DataFrame> { let mut transformed_df = df.clone(); for idx in 0..transformed_df.width() { transformed_df.try_apply_at_idx(idx, |series| { if let Ok(ca) = series.f64() { let ca_filled = ca.fill_null_with_values(0.0); ca_filled } else { series.f64().cloned() } })?; } Ok(transformed_df) } </code>
Some(Commands::Impute { input_path, output_path }) => {
    // Start measuring time
    let start = Instant::now();

    let df = polars2::read_parquet(&input_path);
    let mut imputed_df = polars2::fill_nan_with_zeroes(&df).expect("Error");
    polars2::write_parquet(&mut imputed_df, &output_path);

   // End measuring time
   let duration = start.elapsed();
   println!("Total duration: {:.2} seconds", duration.as_secs_f64());
}

// utilities for working with polars dataframes
//
use polars::prelude::*;
use std::fs::File;
//use polars::df;

//read in a parquet file
pub fn read_parquet(path: &str) -> DataFrame {
    // Open file
    let file = File::open(path).expect("Failed to open file");

    // Read to DataFrame and Return
    ParquetReader::new(file)
        .finish()
        .expect("Failed to read Parquet file")
}

// write dataframe to parquet file
pub fn write_parquet(df: &mut DataFrame, path: &str) {
    // create a file
    let file = File::create(path)
            .expect("could not create output file");

    // write dataframe to output parquet
    ParquetWriter::new(file)
        .finish(df)
        .expect("Failed to write dataframe.");

}

//print "n" rows of a dataframe
pub fn print_df(df: &DataFrame, n: usize) {
    println!("{:?}", df.head(Some(n)));
}

//print the schema of a dataframe
pub fn print_schema(df: &DataFrame) {
    println!("{:?}", df.schema());
}

//print the shape of a dataframe
pub fn print_shape(df: &DataFrame) {
    println!("{:?}", df.shape());
}

/// Replaces NaN with zeroes. 
pub fn fill_nan_with_zeroes(df: &DataFrame) -> PolarsResult<DataFrame> {
    let mut transformed_df = df.clone();

    for idx in 0..transformed_df.width() {
        transformed_df.try_apply_at_idx(idx, |series| {
            if let Ok(ca) = series.f64() {
                let ca_filled = ca.fill_null_with_values(0.0);
                ca_filled
            } else {
                series.f64().cloned()
            }
        })?;
    }

    Ok(transformed_df)
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật