Skip to main content

CsvFile

Struct CsvFile 

Source
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

Source

pub fn new(headers: Vec<String>) -> Self

Create a new CsvFile with the given headers and no records.

Source

pub fn add_record(&mut self, fields: Vec<String>)

Append a record from string fields.

Source

pub fn add_record_f64(&mut self, values: &[f64])

Append a record from f64 values (formatted with full precision).

Source

pub fn record_count(&self) -> usize

Return the number of data records (excluding header).

Source

pub fn column_count(&self) -> usize

Return the number of columns (header count).

Source

pub fn get_column_f64(&self, col_idx: usize) -> Result<Vec<f64>, String>

Extract all values from a column by index as f64.

Source

pub fn get_column_by_name(&self, name: &str) -> Option<usize>

Return the column index of a header name, or None if not found.

Source

pub fn to_string(&self) -> String

Serialize the CSV file to a String (header + records, comma-separated).

Source

pub fn to_string_with_delimiter(&self, delim: char) -> String

Serialize the CSV file using a custom delimiter character.

Source

pub fn from_str(s: &str) -> Result<Self, String>

Parse a CSV string (first line = header, comma delimiter).

Source

pub fn from_str_with_delimiter(s: &str, delim: char) -> Result<Self, String>

Parse a CSV string with a specified delimiter.

Source

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.

Source

pub fn infer_column_type(&self, col_idx: usize) -> ColumnType

Infer the type of a column (Integer, Float, or Text).

Source

pub fn select_columns(&self, col_indices: &[usize]) -> CsvFile

Return a new CsvFile containing only the specified columns (by index).

Source

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).

Source

pub fn normalize_headers(&mut self)

Normalize headers: lowercase, replace spaces/special chars with underscores, strip leading/trailing whitespace.

Source

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.

Source

pub fn all_column_stats(&self) -> Vec<(String, ColumnStats)>

Compute statistics for all columns that are numeric. Returns a vector of (column_name, ColumnStats).

Source

pub fn get_column_strings(&self, col_idx: usize) -> Result<Vec<String>, String>

Extract all values from a column as strings.

Source

pub fn get_column_i64(&self, col_idx: usize) -> Result<Vec<i64>, String>

Extract all values from a column as i64.

Source

pub fn sort_by_column(&mut self, col_idx: usize)

Sort rows by a column (ascending, numeric).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.