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
impl Table
Sourcepub fn from_csv_str(input: &str) -> Result<Table, MattenDataError>
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?
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}Sourcepub fn from_csv_path<P: AsRef<Path>>(path: P) -> Result<Table, MattenDataError>
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
impl Table
Sourcepub fn column_count(&self) -> usize
pub fn column_count(&self) -> usize
Number of columns.
Sourcepub fn column_names(&self) -> &[String]
pub fn column_names(&self) -> &[String]
Column names, in column order.
Sourcepub fn schema_summary(&self) -> SchemaSummary
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?
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}Sourcepub fn select_columns<I, S>(&self, columns: I) -> Result<Table, MattenDataError>
pub fn select_columns<I, S>(&self, columns: I) -> Result<Table, MattenDataError>
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?
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}Sourcepub fn fill_missing(
&self,
value: impl Into<CellValue>,
) -> Result<Table, MattenDataError>
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?
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}Sourcepub fn try_numeric(&self) -> Result<NumericTable, MattenDataError>
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?
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}