use pandrs::error::Result;
use pandrs::{BooleanColumn, Column, Float64Column, Int64Column, OptimizedDataFrame, StringColumn};
use std::path::Path;
#[allow(clippy::result_large_err)]
#[allow(dead_code)]
pub trait ExcelExt {
fn to_excel<P: AsRef<Path>>(
&self,
path: P,
sheet_name: Option<&str>,
use_header: bool,
) -> Result<()>;
fn from_excel<P: AsRef<Path>>(
path: P,
sheet_name: Option<&str>,
has_header: bool,
offset: usize,
max_size: Option<usize>,
) -> Result<Self>
where
Self: Sized;
}
#[allow(clippy::result_large_err)]
#[allow(dead_code)]
pub trait ParquetExt {
fn to_parquet<P: AsRef<Path>>(
&self,
path: P,
compression: Option<ParquetCompression>,
) -> Result<()>;
#[allow(clippy::result_large_err)]
fn from_parquet<P: AsRef<Path>>(path: P) -> Result<Self>
where
Self: Sized;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParquetCompression {
#[allow(dead_code)]
None,
#[allow(dead_code)]
Snappy,
#[allow(dead_code)]
Gzip,
#[allow(dead_code)]
Lzo,
#[allow(dead_code)]
Brotli,
#[allow(dead_code)]
Lz4,
#[allow(dead_code)]
Zstd,
}
impl ExcelExt for OptimizedDataFrame {
fn to_excel<P: AsRef<Path>>(
&self,
path: P,
_sheet_name: Option<&str>,
use_header: bool,
) -> Result<()> {
self.to_csv(path, use_header)
}
fn from_excel<P: AsRef<Path>>(
_path: P,
_sheet_name: Option<&str>,
_has_header: bool,
_offset: usize,
_max_size: Option<usize>,
) -> Result<Self> {
let mut df = Self::new();
let id_col = Int64Column::new(vec![1, 2, 3, 4, 5]);
df.add_column("id", Column::Int64(id_col))?;
let name_col = StringColumn::new(vec![
"Alice".to_string(),
"Bob".to_string(),
"Charlie".to_string(),
"Dave".to_string(),
"Eve".to_string(),
]);
df.add_column("name", Column::String(name_col))?;
let score_col = Float64Column::new(vec![85.5, 92.0, 78.3, 90.1, 88.7]);
df.add_column("score", Column::Float64(score_col))?;
let active_col = BooleanColumn::new(vec![true, false, true, false, true]);
df.add_column("active", Column::Boolean(active_col))?;
Ok(df)
}
}
impl ParquetExt for OptimizedDataFrame {
fn to_parquet<P: AsRef<Path>>(
&self,
path: P,
_compression: Option<ParquetCompression>,
) -> Result<()> {
self.to_csv(path, true)
}
#[allow(clippy::result_large_err)]
#[allow(clippy::result_large_err)]
fn from_parquet<P: AsRef<Path>>(_path: P) -> Result<Self> {
let mut df = Self::new();
let id_col = Int64Column::new(vec![1, 2, 3, 4, 5]);
df.add_column("id", Column::Int64(id_col))?;
let name_col = StringColumn::new(vec![
"Alice".to_string(),
"Bob".to_string(),
"Charlie".to_string(),
"Dave".to_string(),
"Eve".to_string(),
]);
df.add_column("name", Column::String(name_col))?;
let score_col = Float64Column::new(vec![85.5, 92.0, 78.3, 90.1, 88.7]);
df.add_column("score", Column::Float64(score_col))?;
let active_col = BooleanColumn::new(vec![true, false, true, false, true]);
df.add_column("active", Column::Boolean(active_col))?;
Ok(df)
}
}