Skip to main content

Table

Struct Table 

Source
pub struct Table { /* private fields */ }
Expand description

A small, owned, rectangular table-like data set.

External guarantees: row order is preserved; column order is preserved; column names are stable after loading. Operations return new owned Table values; no borrowed view lifetimes appear in normal use.

Implementations§

Source§

impl Table

Source

pub fn from_csv_str(input: &str) -> Result<Table, MattenDataError>

Parse a Table from a CSV string.

The first row is the header. Empty input, empty or duplicate header names, and ragged rows are reported as MattenDataError (never a panic).

use matten_data::Table;
let table = Table::from_csv_str("a,b\n1,2\n3,4").unwrap();
assert_eq!(table.row_count(), 2);
assert_eq!(table.column_names(), &["a".to_string(), "b".to_string()]);
Examples found in repository?
examples/csv_to_tensor.rs (line 15)
7fn main() -> Result<(), matten_data::MattenDataError> {
8    // A small, messy table: mixed types and a missing cell.
9    let csv = "\
10region,sales,cost,quantity
11north,100,40,5
12south,150,,7
13east,120,55,6";
14
15    let table = Table::from_csv_str(csv)?;
16
17    // Inspect what we have before converting anything.
18    println!("{}", table.schema_summary());
19
20    // Select only the numeric columns we want, fill the one missing cost with 0,
21    // convert explicitly, and produce a [rows, columns] f64 tensor.
22    let tensor = table
23        .select_columns(["sales", "cost", "quantity"])?
24        .fill_missing(0.0)?
25        .try_numeric()?
26        .to_tensor()?;
27
28    println!("tensor shape: {:?}", tensor.shape());
29    println!("tensor data : {:?}", tensor.as_slice());
30
31    assert_eq!(tensor.shape(), &[3, 3]);
32    Ok(())
33}
Source

pub fn from_csv_path<P: AsRef<Path>>(path: P) -> Result<Table, MattenDataError>

Parse a Table from a CSV file at path.

I/O failures (for example a missing file) are reported as MattenDataError::Io with the path and underlying error preserved.

Source§

impl Table

Source

pub fn row_count(&self) -> usize

Number of data rows.

Source

pub fn column_count(&self) -> usize

Number of columns.

Source

pub fn column_names(&self) -> &[String]

Column names, in column order.

Source

pub fn schema_summary(&self) -> SchemaSummary

A small, displayable schema summary (row/column counts, per-column missing counts and inferred kinds). Does not perform expensive analysis.

Examples found in repository?
examples/csv_to_tensor.rs (line 18)
7fn main() -> Result<(), matten_data::MattenDataError> {
8    // A small, messy table: mixed types and a missing cell.
9    let csv = "\
10region,sales,cost,quantity
11north,100,40,5
12south,150,,7
13east,120,55,6";
14
15    let table = Table::from_csv_str(csv)?;
16
17    // Inspect what we have before converting anything.
18    println!("{}", table.schema_summary());
19
20    // Select only the numeric columns we want, fill the one missing cost with 0,
21    // convert explicitly, and produce a [rows, columns] f64 tensor.
22    let tensor = table
23        .select_columns(["sales", "cost", "quantity"])?
24        .fill_missing(0.0)?
25        .try_numeric()?
26        .to_tensor()?;
27
28    println!("tensor shape: {:?}", tensor.shape());
29    println!("tensor data : {:?}", tensor.as_slice());
30
31    assert_eq!(tensor.shape(), &[3, 3]);
32    Ok(())
33}
Source

pub fn select_columns<I, S>(&self, columns: I) -> Result<Table, MattenDataError>
where I: IntoIterator<Item = S>, S: AsRef<str>,

Select columns by name, returning a new Table.

Behavior (RFC-034 §5.3): preserves the requested column order; errors with MattenDataError::MissingColumn if a requested column does not exist; rejects duplicate selections with MattenDataError::DuplicateSelection; an empty selection is MattenDataError::EmptySelection.

Examples found in repository?
examples/csv_to_tensor.rs (line 23)
7fn main() -> Result<(), matten_data::MattenDataError> {
8    // A small, messy table: mixed types and a missing cell.
9    let csv = "\
10region,sales,cost,quantity
11north,100,40,5
12south,150,,7
13east,120,55,6";
14
15    let table = Table::from_csv_str(csv)?;
16
17    // Inspect what we have before converting anything.
18    println!("{}", table.schema_summary());
19
20    // Select only the numeric columns we want, fill the one missing cost with 0,
21    // convert explicitly, and produce a [rows, columns] f64 tensor.
22    let tensor = table
23        .select_columns(["sales", "cost", "quantity"])?
24        .fill_missing(0.0)?
25        .try_numeric()?
26        .to_tensor()?;
27
28    println!("tensor shape: {:?}", tensor.shape());
29    println!("tensor data : {:?}", tensor.as_slice());
30
31    assert_eq!(tensor.shape(), &[3, 3]);
32    Ok(())
33}
Source

pub fn fill_missing( &self, value: impl Into<CellValue>, ) -> Result<Table, MattenDataError>

Fill every missing cell with value, returning a new Table.

Missing values are never silently turned into zero; filling is always explicit (RFC-035 §6). Non-missing cells and the shape are unchanged.

Examples found in repository?
examples/csv_to_tensor.rs (line 24)
7fn main() -> Result<(), matten_data::MattenDataError> {
8    // A small, messy table: mixed types and a missing cell.
9    let csv = "\
10region,sales,cost,quantity
11north,100,40,5
12south,150,,7
13east,120,55,6";
14
15    let table = Table::from_csv_str(csv)?;
16
17    // Inspect what we have before converting anything.
18    println!("{}", table.schema_summary());
19
20    // Select only the numeric columns we want, fill the one missing cost with 0,
21    // convert explicitly, and produce a [rows, columns] f64 tensor.
22    let tensor = table
23        .select_columns(["sales", "cost", "quantity"])?
24        .fill_missing(0.0)?
25        .try_numeric()?
26        .to_tensor()?;
27
28    println!("tensor shape: {:?}", tensor.shape());
29    println!("tensor data : {:?}", tensor.as_slice());
30
31    assert_eq!(tensor.shape(), &[3, 3]);
32    Ok(())
33}
Source

pub fn try_numeric(&self) -> Result<NumericTable, MattenDataError>

Convert the table to an explicit numeric table (RFC-035 §7).

Strict conversion: Int/Float become f64; Bool and Text are rejected (MattenDataError::NonNumericValue); a remaining Missing cell is rejected (MattenDataError::MissingValue). Text is never parsed as a number by default.

Examples found in repository?
examples/csv_to_tensor.rs (line 25)
7fn main() -> Result<(), matten_data::MattenDataError> {
8    // A small, messy table: mixed types and a missing cell.
9    let csv = "\
10region,sales,cost,quantity
11north,100,40,5
12south,150,,7
13east,120,55,6";
14
15    let table = Table::from_csv_str(csv)?;
16
17    // Inspect what we have before converting anything.
18    println!("{}", table.schema_summary());
19
20    // Select only the numeric columns we want, fill the one missing cost with 0,
21    // convert explicitly, and produce a [rows, columns] f64 tensor.
22    let tensor = table
23        .select_columns(["sales", "cost", "quantity"])?
24        .fill_missing(0.0)?
25        .try_numeric()?
26        .to_tensor()?;
27
28    println!("tensor shape: {:?}", tensor.shape());
29    println!("tensor data : {:?}", tensor.as_slice());
30
31    assert_eq!(tensor.shape(), &[3, 3]);
32    Ok(())
33}

Trait Implementations§

Source§

impl Clone for Table

Source§

fn clone(&self) -> Table

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Table

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Table

§

impl RefUnwindSafe for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnsafeUnpin for Table

§

impl UnwindSafe for Table

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.