Skip to main content

Table

Struct Table 

Source
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

Source

pub fn new() -> Self

Source

pub fn add_column(&mut self, column: AnyDataArray)

Add a column. All columns must have the same number of tuples (rows).

Source

pub fn num_rows(&self) -> usize

Number of rows.

Source

pub fn num_columns(&self) -> usize

Number of columns.

Source

pub fn column(&self, idx: usize) -> Option<&AnyDataArray>

Get column by index.

Source

pub fn column_by_name(&self, name: &str) -> Option<&AnyDataArray>

Get column by name.

Source

pub fn column_names(&self) -> Vec<&str>

Get column names.

Source

pub fn columns(&self) -> &[AnyDataArray]

Iterate over columns.

Source

pub fn value_f64(&self, row: usize, column_name: &str) -> Option<f64>

Get a single scalar value at (row, column_name).

Source

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.

Source

pub fn sort_by_column(&self, column_name: &str) -> Vec<usize>

Get row indices sorted by a column’s values (ascending).

Source

pub fn select_rows(&self, indices: &[usize]) -> Table

Extract a subset of rows by indices, returning a new Table.

Source

pub fn remove_column(&mut self, name: &str) -> Option<AnyDataArray>

Remove a column by name. Returns the removed column if found.

Source

pub fn with_column(self, column: AnyDataArray) -> Self

Builder: add a column.

Source

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"));
Source

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.

Source

pub fn write_csv_file(&self, path: &Path) -> Result<()>

Write CSV to a file path.

Source

pub fn read_csv_file(path: &Path) -> Result<Self, String>

Read CSV from a file path.

Trait Implementations§

Source§

impl Clone for Table

Source§

fn clone(&self) -> Table

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl DataObject for Table

Source§

impl Debug for Table

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Table

Source§

fn default() -> Table

Returns the “default value” for a type. Read more
Source§

impl Display 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.