Crate polars_excel_writer
source ·Expand description
A crate for serializing Polars dataframes to Excel Xlsx files.
The polars_excel_writer
crate provides two interfaces for writing a
dataframe to an Excel Xlsx file:
ExcelWriter
a simple Excel serializer that implements the PolarsSerWriter
trait to write a dataframe to an Excel Xlsx file.PolarsXlsxWriter
a more configurable Excel serializer that more closely resembles the interface options provided by the Polars Pythonwrite_excel()
dataframe method.
ExcelWriter
uses PolarsXlsxWriter
to do the Excel serialization which in
turn uses the rust_xlsxwriter
crate.
§Examples
An example of writing a Polar Rust dataframe to an Excel file using the
ExcelWriter
and PolarsXlsxWriter
interfaces.
use chrono::prelude::*;
use polars::prelude::*;
fn main() {
// Create a sample dataframe for the example.
let mut 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(),
],
)
.unwrap();
example1(&mut df).unwrap();
example2(&df).unwrap();
}
// The ExcelWriter interface.
use polars_excel_writer::ExcelWriter;
fn example1(df: &mut DataFrame) -> PolarsResult<()> {
let mut file = std::fs::File::create("dataframe.xlsx").unwrap();
ExcelWriter::new(&mut file).finish(df)
}
// The PolarsXlsxWriter interface. For this simple case it is similar to the
// ExcelWriter interface but it has additional options to support more complex
// use cases.
use polars_excel_writer::PolarsXlsxWriter;
fn example2(df: &DataFrame) -> PolarsResult<()> {
let mut xlsx_writer = PolarsXlsxWriter::new();
xlsx_writer.write_dataframe(df)?;
xlsx_writer.save("dataframe2.xlsx")?;
Ok(())
}
Second output file (same as the first):
§Performance
The table below shows the performance of writing a dataframe using Python
Polars, Python Pandas and PolarsXlsxWriter
.
-
Performance data:
Test Case Time (s) Relative (%) Polars
6.49 100% Pandas
10.92 168% polars_excel_writer
1.22 19% polars_excel_writer
+zlib
1.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 thePolarsXlsxWriter
interface. Seeperf_test.rs
.polars_excel_writer
+zlib
: Same as the previous test case but uses thezlib
feature 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.
Re-exports§
pub use ExcelWriter;
pub use PolarsXlsxWriter;
Modules§
- A module that exports the
ExcelWriter
struct which implements the PolarsSerWriter
trait to serialize a dataframe to an Excel Xlsx file. - A module that exports the
PolarsXlsxWriter
struct which provides an Excel Xlsx serializer that works with Polars dataframes and which can also interact with therust_xlsxwriter
writing engine that it wraps.