Expand description
A crate for serializing Polars dataframes to Excel Xlsx files.
The polars_excel_writer crate provides a primary interface
PolarsExcelWriter which is a configurable Excel serializer that
resembles the interface options provided by the Polars write_excel()
dataframe method.
This crate uses rust_xlsxwriter to do the Excel serialization and is
typically 5x faster than Polars when exporting large dataframes to Excel,
see the Performance data below.
§Examples
An example of writing a Polar Rust dataframe to an Excel file:.
use chrono::prelude::*;
use polars::prelude::*;
use polars_excel_writer::PolarsExcelWriter;
fn main() -> PolarsResult<()> {
// Create a sample dataframe for the example.
let df: DataFrame = df!(
"String" => &["North", "South", "East", "West"],
"Integer" => &[1, 2, 3, 4],
"Float" => &[4.0, 5.0, 6.0, 7.0],
"Time" => &[
NaiveTime::from_hms_milli_opt(2, 59, 3, 456).unwrap(),
NaiveTime::from_hms_milli_opt(2, 59, 3, 456).unwrap(),
NaiveTime::from_hms_milli_opt(2, 59, 3, 456).unwrap(),
NaiveTime::from_hms_milli_opt(2, 59, 3, 456).unwrap(),
],
"Date" => &[
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap(),
NaiveDate::from_ymd_opt(2022, 1, 2).unwrap(),
NaiveDate::from_ymd_opt(2022, 1, 3).unwrap(),
NaiveDate::from_ymd_opt(2022, 1, 4).unwrap(),
],
"Datetime" => &[
NaiveDate::from_ymd_opt(2022, 1, 1).unwrap().and_hms_opt(1, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2022, 1, 2).unwrap().and_hms_opt(2, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2022, 1, 3).unwrap().and_hms_opt(3, 0, 0).unwrap(),
NaiveDate::from_ymd_opt(2022, 1, 4).unwrap().and_hms_opt(4, 0, 0).unwrap(),
],
)?;
// Create a new Excel writer.
let mut excel_writer = PolarsExcelWriter::new();
// Write the dataframe to Excel.
excel_writer.write_dataframe(&df)?;
// Save the file to disk.
excel_writer.save("dataframe.xlsx")?;
Ok(())
}Output file:
See the PolarsExcelWriter documentation section for more details.
§Performance
The table below shows the performance of writing a dataframe using Python
Polars, Python Pandas and PolarsExcelWriter.
-
Performance data:
Test Case Time (s) Relative (%) Polars6.49 100% Pandas10.92 168% polars_excel_writer1.22 19% polars_excel_writer+zlib1.08 17%
The tested configurations were:
Polars: The dataframe was created in Python Polars and written using thewrite_excel()function. Seeperf_test.py.Pandas: The dataframe was created in Polars but converted to Pandas and then written via the Pandasto_excel()function. See alsoperf_test.py.polars_excel_writer: The dataframe was created in Rust Polars and written using thePolarsExcelWriterinterface. Seeperf_test.rs.polars_excel_writer+zlib: Same as the previous test case but uses thezlibfeature flag to enable the C zlib library in conjunction with the backendZipWriter.
Note: The performance was tested for the dataframe writing code only. The code used to create the dataframes was omitted from the test results.
§See also
Changelog: Release notes and changelog.PolarsExcelWriter: The main API documentation for writing Polars dataframes to Excel files.polars_comparison: A comparison betweenPolarsExcelWriterand the Polarswrite_excel()function.
Modules§
- changelog
- Changes and fixes in the
polars_excel_writerlibrary. - excel_
writer - A module that exports the
PolarsExcelWriterstruct which provides the primary Excel Xlsx serializer that works with Polars dataframes and which can also interact with therust_xlsxwriterwriting engine that it wraps. - polars_
comparison - Comparison of
PolarsExcelWriterandPolarswrite_excel().