pub struct Table { /* private fields */ }Expand description
Columnar data table (rows × named columns).
Analogous to VTK’s vtkTable. Each column is an AnyDataArray where
the number of tuples is the number of rows.
§Examples
use crate::data::{Table, AnyDataArray, DataArray};
let table = Table::new()
.with_column(AnyDataArray::F64(DataArray::from_vec("x", vec![1.0, 2.0, 3.0], 1)))
.with_column(AnyDataArray::F64(DataArray::from_vec("y", vec![4.0, 5.0, 6.0], 1)));
assert_eq!(table.num_rows(), 3);
assert_eq!(table.num_columns(), 2);
assert_eq!(table.value_f64(1, "x"), Some(2.0));Implementations§
Source§impl Table
impl Table
pub fn new() -> Self
Sourcepub fn add_column(&mut self, column: AnyDataArray)
pub fn add_column(&mut self, column: AnyDataArray)
Add a column. All columns must have the same number of tuples (rows).
Sourcepub fn num_columns(&self) -> usize
pub fn num_columns(&self) -> usize
Number of columns.
Sourcepub fn column(&self, idx: usize) -> Option<&AnyDataArray>
pub fn column(&self, idx: usize) -> Option<&AnyDataArray>
Get column by index.
Sourcepub fn column_by_name(&self, name: &str) -> Option<&AnyDataArray>
pub fn column_by_name(&self, name: &str) -> Option<&AnyDataArray>
Get column by name.
Sourcepub fn column_names(&self) -> Vec<&str>
pub fn column_names(&self) -> Vec<&str>
Get column names.
Sourcepub fn columns(&self) -> &[AnyDataArray]
pub fn columns(&self) -> &[AnyDataArray]
Iterate over columns.
Sourcepub fn value_f64(&self, row: usize, column_name: &str) -> Option<f64>
pub fn value_f64(&self, row: usize, column_name: &str) -> Option<f64>
Get a single scalar value at (row, column_name).
Sourcepub fn filter_rows(
&self,
column_name: &str,
predicate: impl Fn(f64) -> bool,
) -> Vec<usize>
pub fn filter_rows( &self, column_name: &str, predicate: impl Fn(f64) -> bool, ) -> Vec<usize>
Get row indices where a scalar column satisfies a predicate.
Sourcepub fn sort_by_column(&self, column_name: &str) -> Vec<usize>
pub fn sort_by_column(&self, column_name: &str) -> Vec<usize>
Get row indices sorted by a column’s values (ascending).
Sourcepub fn select_rows(&self, indices: &[usize]) -> Table
pub fn select_rows(&self, indices: &[usize]) -> Table
Extract a subset of rows by indices, returning a new Table.
Sourcepub fn remove_column(&mut self, name: &str) -> Option<AnyDataArray>
pub fn remove_column(&mut self, name: &str) -> Option<AnyDataArray>
Remove a column by name. Returns the removed column if found.
Sourcepub fn with_column(self, column: AnyDataArray) -> Self
pub fn with_column(self, column: AnyDataArray) -> Self
Builder: add a column.
Sourcepub fn to_csv<W: Write>(&self, w: &mut W) -> Result<()>
pub fn to_csv<W: Write>(&self, w: &mut W) -> Result<()>
Write the table as CSV to a writer.
use crate::data::{Table, AnyDataArray, DataArray};
let table = Table::new()
.with_column(AnyDataArray::F64(DataArray::from_vec("x", vec![1.0, 2.0], 1)))
.with_column(AnyDataArray::F64(DataArray::from_vec("y", vec![3.0, 4.0], 1)));
let mut buf = Vec::new();
table.to_csv(&mut buf).unwrap();
let csv = String::from_utf8(buf).unwrap();
assert!(csv.contains("x,y"));Sourcepub fn from_csv<R: BufRead>(r: R) -> Result<Self, String>
pub fn from_csv<R: BufRead>(r: R) -> Result<Self, String>
Read a table from CSV. First line is header (column names). All values are parsed as f64.
Sourcepub fn write_csv_file(&self, path: &Path) -> Result<()>
pub fn write_csv_file(&self, path: &Path) -> Result<()>
Write CSV to a file path.
Sourcepub fn read_csv_file(path: &Path) -> Result<Self, String>
pub fn read_csv_file(path: &Path) -> Result<Self, String>
Read CSV from a file path.
Trait Implementations§
Source§impl DataObject for Table
impl DataObject for Table
fn field_data(&self) -> &FieldData
fn field_data_mut(&mut self) -> &mut FieldData
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> 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more