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?
19fn main() -> Result<(), matten_data::MattenDataError> {
20 // A small, messy table: a text column and one missing numeric cell.
21 let csv = "\
22region,sales,cost,quantity
23north,100,40,5
24south,150,,7
25east,120,55,6";
26
27 let table = Table::from_csv_str(csv)?;
28
29 // Inspect what we have before converting anything.
30 println!("{}", table.schema_summary());
31
32 // Select only the numeric columns we want, fill the one missing cost with 0,
33 // convert explicitly, and produce a [rows, columns] f64 tensor.
34 let tensor = table
35 .select_columns(["sales", "cost", "quantity"])?
36 .fill_missing(0.0)?
37 .try_numeric()?
38 .to_tensor()?;
39
40 println!("tensor shape: {:?}", tensor.shape());
41 println!("tensor data : {:?}", tensor.as_slice());
42
43 // 3 rows x 3 columns; the missing south/cost became 0.0.
44 assert_eq!(tensor.shape(), &[3, 3]);
45 assert_eq!(
46 tensor.as_slice(),
47 &[100.0, 40.0, 5.0, 150.0, 0.0, 7.0, 120.0, 55.0, 6.0]
48 );
49 println!("csv_to_tensor: OK");
50 Ok(())
51}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?
19fn main() -> Result<(), matten_data::MattenDataError> {
20 // A small, messy table: a text column and one missing numeric cell.
21 let csv = "\
22region,sales,cost,quantity
23north,100,40,5
24south,150,,7
25east,120,55,6";
26
27 let table = Table::from_csv_str(csv)?;
28
29 // Inspect what we have before converting anything.
30 println!("{}", table.schema_summary());
31
32 // Select only the numeric columns we want, fill the one missing cost with 0,
33 // convert explicitly, and produce a [rows, columns] f64 tensor.
34 let tensor = table
35 .select_columns(["sales", "cost", "quantity"])?
36 .fill_missing(0.0)?
37 .try_numeric()?
38 .to_tensor()?;
39
40 println!("tensor shape: {:?}", tensor.shape());
41 println!("tensor data : {:?}", tensor.as_slice());
42
43 // 3 rows x 3 columns; the missing south/cost became 0.0.
44 assert_eq!(tensor.shape(), &[3, 3]);
45 assert_eq!(
46 tensor.as_slice(),
47 &[100.0, 40.0, 5.0, 150.0, 0.0, 7.0, 120.0, 55.0, 6.0]
48 );
49 println!("csv_to_tensor: OK");
50 Ok(())
51}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?
19fn main() -> Result<(), matten_data::MattenDataError> {
20 // A small, messy table: a text column and one missing numeric cell.
21 let csv = "\
22region,sales,cost,quantity
23north,100,40,5
24south,150,,7
25east,120,55,6";
26
27 let table = Table::from_csv_str(csv)?;
28
29 // Inspect what we have before converting anything.
30 println!("{}", table.schema_summary());
31
32 // Select only the numeric columns we want, fill the one missing cost with 0,
33 // convert explicitly, and produce a [rows, columns] f64 tensor.
34 let tensor = table
35 .select_columns(["sales", "cost", "quantity"])?
36 .fill_missing(0.0)?
37 .try_numeric()?
38 .to_tensor()?;
39
40 println!("tensor shape: {:?}", tensor.shape());
41 println!("tensor data : {:?}", tensor.as_slice());
42
43 // 3 rows x 3 columns; the missing south/cost became 0.0.
44 assert_eq!(tensor.shape(), &[3, 3]);
45 assert_eq!(
46 tensor.as_slice(),
47 &[100.0, 40.0, 5.0, 150.0, 0.0, 7.0, 120.0, 55.0, 6.0]
48 );
49 println!("csv_to_tensor: OK");
50 Ok(())
51}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?
19fn main() -> Result<(), matten_data::MattenDataError> {
20 // A small, messy table: a text column and one missing numeric cell.
21 let csv = "\
22region,sales,cost,quantity
23north,100,40,5
24south,150,,7
25east,120,55,6";
26
27 let table = Table::from_csv_str(csv)?;
28
29 // Inspect what we have before converting anything.
30 println!("{}", table.schema_summary());
31
32 // Select only the numeric columns we want, fill the one missing cost with 0,
33 // convert explicitly, and produce a [rows, columns] f64 tensor.
34 let tensor = table
35 .select_columns(["sales", "cost", "quantity"])?
36 .fill_missing(0.0)?
37 .try_numeric()?
38 .to_tensor()?;
39
40 println!("tensor shape: {:?}", tensor.shape());
41 println!("tensor data : {:?}", tensor.as_slice());
42
43 // 3 rows x 3 columns; the missing south/cost became 0.0.
44 assert_eq!(tensor.shape(), &[3, 3]);
45 assert_eq!(
46 tensor.as_slice(),
47 &[100.0, 40.0, 5.0, 150.0, 0.0, 7.0, 120.0, 55.0, 6.0]
48 );
49 println!("csv_to_tensor: OK");
50 Ok(())
51}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?
19fn main() -> Result<(), matten_data::MattenDataError> {
20 // A small, messy table: a text column and one missing numeric cell.
21 let csv = "\
22region,sales,cost,quantity
23north,100,40,5
24south,150,,7
25east,120,55,6";
26
27 let table = Table::from_csv_str(csv)?;
28
29 // Inspect what we have before converting anything.
30 println!("{}", table.schema_summary());
31
32 // Select only the numeric columns we want, fill the one missing cost with 0,
33 // convert explicitly, and produce a [rows, columns] f64 tensor.
34 let tensor = table
35 .select_columns(["sales", "cost", "quantity"])?
36 .fill_missing(0.0)?
37 .try_numeric()?
38 .to_tensor()?;
39
40 println!("tensor shape: {:?}", tensor.shape());
41 println!("tensor data : {:?}", tensor.as_slice());
42
43 // 3 rows x 3 columns; the missing south/cost became 0.0.
44 assert_eq!(tensor.shape(), &[3, 3]);
45 assert_eq!(
46 tensor.as_slice(),
47 &[100.0, 40.0, 5.0, 150.0, 0.0, 7.0, 120.0, 55.0, 6.0]
48 );
49 println!("csv_to_tensor: OK");
50 Ok(())
51}