use crate::dataframe::DataFrame;
use crate::series::Series;
use std::fmt;
impl fmt::Display for DataFrame {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.row_count() == 0 {
return write!(f, "Empty DataFrame");
}
let column_names: Vec<&String> = self.columns.keys().collect();
for name in &column_names {
write!(f, "{name: <15}")?;
}
writeln!(f)?;
for _ in &column_names {
write!(f, "--------------- ")?;
}
writeln!(f)?;
for i in 0..self.row_count() {
for name in &column_names {
let series = self.columns.get(*name).unwrap();
let value_str = match series {
Series::I32(_, v, _) => v.get(i).map_or("null".to_string(), |i| i.to_string()),
Series::F64(_, v, _) => v.get(i).map_or("null".to_string(), |f| f.to_string()),
Series::Bool(_, v, _) => v.get(i).map_or("null".to_string(), |b| b.to_string()),
Series::String(_, v, _) => v.get(i).map_or("null".to_string(), |s| s.clone()),
Series::DateTime(_, v, _) => {
v.get(i).map_or("null".to_string(), |t| t.to_string())
}
};
write!(f, "{value_str: <15}")?;
}
writeln!(f)?;
}
Ok(())
}
}