pub struct CsvFile {
pub headers: Vec<String>,
pub records: Vec<CsvRecord>,
}Expand description
An in-memory representation of a CSV file with headers.
Fields§
§headers: Vec<String>Column header names.
records: Vec<CsvRecord>All data records.
Implementations§
Source§impl CsvFile
impl CsvFile
Sourcepub fn new(headers: Vec<String>) -> Self
pub fn new(headers: Vec<String>) -> Self
Create a new CsvFile with the given headers and no records.
Sourcepub fn add_record(&mut self, fields: Vec<String>)
pub fn add_record(&mut self, fields: Vec<String>)
Append a record from string fields.
Sourcepub fn add_record_f64(&mut self, values: &[f64])
pub fn add_record_f64(&mut self, values: &[f64])
Append a record from f64 values (formatted with full precision).
Sourcepub fn record_count(&self) -> usize
pub fn record_count(&self) -> usize
Return the number of data records (excluding header).
Sourcepub fn column_count(&self) -> usize
pub fn column_count(&self) -> usize
Return the number of columns (header count).
Sourcepub fn get_column_f64(&self, col_idx: usize) -> Result<Vec<f64>, String>
pub fn get_column_f64(&self, col_idx: usize) -> Result<Vec<f64>, String>
Extract all values from a column by index as f64.
Sourcepub fn get_column_by_name(&self, name: &str) -> Option<usize>
pub fn get_column_by_name(&self, name: &str) -> Option<usize>
Return the column index of a header name, or None if not found.
Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Serialize the CSV file to a String (header + records, comma-separated).
Sourcepub fn to_string_with_delimiter(&self, delim: char) -> String
pub fn to_string_with_delimiter(&self, delim: char) -> String
Serialize the CSV file using a custom delimiter character.
Sourcepub fn from_str(s: &str) -> Result<Self, String>
pub fn from_str(s: &str) -> Result<Self, String>
Parse a CSV string (first line = header, comma delimiter).
Sourcepub fn from_str_with_delimiter(s: &str, delim: char) -> Result<Self, String>
pub fn from_str_with_delimiter(s: &str, delim: char) -> Result<Self, String>
Parse a CSV string with a specified delimiter.
Sourcepub fn filter_rows(&self, col_idx: usize, pred: impl Fn(f64) -> bool) -> CsvFile
pub fn filter_rows(&self, col_idx: usize, pred: impl Fn(f64) -> bool) -> CsvFile
Return a new CsvFile containing only rows where pred(value) is true
for the value in col_idx. Rows where parsing fails are excluded.
Sourcepub fn infer_column_type(&self, col_idx: usize) -> ColumnType
pub fn infer_column_type(&self, col_idx: usize) -> ColumnType
Infer the type of a column (Integer, Float, or Text).
Sourcepub fn select_columns(&self, col_indices: &[usize]) -> CsvFile
pub fn select_columns(&self, col_indices: &[usize]) -> CsvFile
Return a new CsvFile containing only the specified columns (by index).
Sourcepub fn select_columns_by_name(&self, names: &[&str]) -> CsvFile
pub fn select_columns_by_name(&self, names: &[&str]) -> CsvFile
Return a new CsvFile containing only columns whose headers match
the given names (preserving the order of names).
Sourcepub fn normalize_headers(&mut self)
pub fn normalize_headers(&mut self)
Normalize headers: lowercase, replace spaces/special chars with underscores, strip leading/trailing whitespace.
Sourcepub fn column_stats(&self, col_idx: usize) -> Option<ColumnStats>
pub fn column_stats(&self, col_idx: usize) -> Option<ColumnStats>
Compute statistics (min, max, mean, sum, count) for a numeric column.
Returns None if no numeric values are found.
Sourcepub fn all_column_stats(&self) -> Vec<(String, ColumnStats)>
pub fn all_column_stats(&self) -> Vec<(String, ColumnStats)>
Compute statistics for all columns that are numeric.
Returns a vector of (column_name, ColumnStats).
Sourcepub fn get_column_strings(&self, col_idx: usize) -> Result<Vec<String>, String>
pub fn get_column_strings(&self, col_idx: usize) -> Result<Vec<String>, String>
Extract all values from a column as strings.
Sourcepub fn get_column_i64(&self, col_idx: usize) -> Result<Vec<i64>, String>
pub fn get_column_i64(&self, col_idx: usize) -> Result<Vec<i64>, String>
Extract all values from a column as i64.
Sourcepub fn sort_by_column(&mut self, col_idx: usize)
pub fn sort_by_column(&mut self, col_idx: usize)
Sort rows by a column (ascending, numeric).
Auto Trait Implementations§
impl Freeze for CsvFile
impl RefUnwindSafe for CsvFile
impl Send for CsvFile
impl Sync for CsvFile
impl Unpin for CsvFile
impl UnsafeUnpin for CsvFile
impl UnwindSafe for CsvFile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.