packages

table::frame

On this page

The table: named, ordered columns of one length, and the operations that only reshape rows or columns (selection, slicing, nulls).

Structs

TableSource

pub struct Table

Named, ordered columns of equal length. Copies share the columns; every operation returns a new table and leaves its input as it was.

nabi
use table::{col, Table};
let t = table::table!{
    "city": ["Seoul", "Busan", "Seoul"],
    "temp": [21.5, 24.0, 19.0],
};
let warm = try t.filter(col("temp") .> 20.0) catch _ { t };
assert(warm.nrows() == 2);
  • pub fn new() -> Table§

    Source

    A table with no columns and no rows.

  • pub fn from_columns(cols: List<(str, Column)>) throws (TableError) -> Table§

    Source

    From (name, column) pairs; every column must have one length and every name be new.

    nabi
    use table::{Column, Table};
    let t = try Table::from_columns([("x", Column::from([1, 2, 3]))]) catch _ { Table::new() };
    assert(t.shape() == (3, 1));
    
  • pub fn from_rows(names: List<str>, rows: List<List<Value>>) throws (TableError) -> Table§

    Source

    Rows of cells against names; each column's type comes from its cells (Column::from_values). A row of another length is an error naming it.

    nabi
    use table::Value;
    let t = try table::Table::from_rows(["a", "b"], [[Value::Int(1), Value::Str("x")], [Value::Int(2), Value::Null]]) catch _ { table::Table::new() };
    assert(t.column_at(1).null_count() == 1);
    
  • pub fn nrows(self) -> usize§

    Source

    The number of rows.

  • pub fn ncols(self) -> usize§

    Source

    The number of columns.

  • pub fn shape(self) -> (usize, usize)§

    Source

    (rows, columns).

  • pub fn is_empty(self) -> bool§

    Source

    No rows.

  • pub fn names(self) -> List<str>§

    Source

    The column names, in order.

  • pub fn schema(self) -> Schema§

    Source

    Names and types.

  • pub fn columns(self) -> List<Column>§

    Source

    The columns, in order.

  • pub fn index_of(self, name: str) throws (TableError) -> usize§

    Source

    The position of column name.

  • pub fn has(self, name: str) -> bool§

    Source

    Whether there is a column name.

  • pub fn column(self, name: str) throws (TableError) -> Column§

    Source

    Column name; a missing name is an error that suggests the closest one.

  • pub fn column_at(self, k: usize) -> Column§

    Source

    Column k (panics past ncols(), as list indexing does).

  • pub fn f64(self, name: str) throws (TableError) -> Array<f64, 1>§

    Source

    Column name as an f64 array (no copy for an f64 column without nulls, so treat it as read-only; nulls read as NaN).

    nabi
    let t = table::table!{ "x": [1.0, 2.0, 3.0] };
    let x = try t.f64("x") catch _ { [0.0] };
    assert((x .* 2.0).sum() == 12.0);
    
  • pub fn i64(self, name: str) throws (TableError) -> Array<i64, 1>§

    Source

    Column name as an i64 array (no copy, read-only).

  • pub fn row(self, i: usize) -> Row§

    Source

    Row i (panics past nrows()).

  • pub fn rows(self) -> List<Row>§

    Source

    Every row, front to back.

  • pub fn get(self, i: usize, name: str) throws (TableError) -> Value§

    Source

    The cell at row i, column name.

  • pub fn with(self, name: str, c: Column) throws (TableError) -> Table§

    Source

    The table with column name set to c: replaced in place when it exists, else added at the end.

    nabi
    use table::Column;
    let t = table::table!{ "a": [1, 2] };
    let u = try t.with("b", Column::from([true, false])) catch _ { t };
    assert(u.names() == ["a", "b"]);
    
  • pub fn with_column(self, name: str, e: Expr) throws (TableError) -> Table§

    Source

    The table with column name computed from e over this table (replaced when it exists).

    nabi
    use table::col;
    let t = table::table!{ "x": [1.0, 2.0], "y": [10.0, 20.0] };
    let u = try t.with_column("z", col("x") .* 2 .+ col("y")) catch _ { t };
    assert(u.row(1).f64("z") == 24.0);
    
  • pub fn with_columns(self, es: List<(str, Expr)>) throws (TableError) -> Table§

    Source

    Several computed columns, each evaluated against this table.

  • pub fn select(self, names: List<str>) throws (TableError) -> Table§

    Source

    Only the columns names, in that order.

    nabi
    let t = table::table!{ "a": [1], "b": [2], "c": [3] };
    let u = try t.select(["c", "a"]) catch _ { t };
    assert(u.names() == ["c", "a"]);
    
  • pub fn drop(self, names: List<str>) throws (TableError) -> Table§

    Source

    Every column except names.

  • pub fn rename(self, pairs: List<(str, str)>) throws (TableError) -> Table§

    Source

    Rename columns by (old, new) pairs.

    nabi
    let t = table::table!{ "a": [1] };
    let u = try t.rename([("a", "id")]) catch _ { t };
    assert(u.names() == ["id"]);
    
  • pub fn head(self, n: usize) -> Table§

    Source

    The first n rows (all when there are fewer).

  • pub fn tail(self, n: usize) -> Table§

    Source

    The last n rows.

  • pub fn slice(self, start: usize, end: usize) -> Table§

    Source

    Rows start..end, clamped to the table; the columns share their buffers with this table.

  • pub fn take(self, idx: List<usize>) throws (TableError) -> Table§

    Source

    The rows at idx, in that order (repeats allowed). An index past the end is an error.

  • pub fn sample(self, n: usize, seed: u64) -> Table§

    Source

    n rows drawn without replacement (all, shuffled, when n is at least the row count), reproducible for one seed. Floyd's algorithm: O(n) draws, kept in draw order.

    nabi
    let t = table::table!{ "a": [1, 2, 3, 4, 5] };
    assert(t.sample(3, 7).nrows() == 3);
    
  • pub fn filter(self, e: Expr) throws (TableError) -> Table§

    Source

    The rows where e is true (null counts as false).

    nabi
    use table::col;
    let t = table::table!{ "x": [1, 5, 3], "tag": ["a", "b", "a"] };
    let u = try t.filter(col("x") .> 2 .&& col("tag") .== "a") catch _ { t };
    assert(u.nrows() == 1);
    
  • pub fn filter_by_column(self, name: str) throws (TableError) -> Table§

    Source

    The rows where boolean column name is true.

  • pub fn filter_mask(self, mask: Array<bool, 1>) throws (TableError) -> Table§

    Source

    The rows where mask is true; its length must be the row count.

  • pub fn filter_rows(self, keep: fn(Row) -> bool) -> Table§

    Source

    The rows for which keep is true.

    nabi
    let t = table::table!{ "name": ["ann", "bob"] };
    let u = t.filter_rows(|r| r.str("name").starts_with("b"));
    assert(u.nrows() == 1);
    
  • pub fn drop_nulls(self, names: List<str> | none) throws (TableError) -> Table§

    Source

    The table with the rows that have a null in any of names removed (none: in any column).

  • pub fn fill_null(self, name: str, v: Value) throws (TableError) -> Table§

    Source

    The table with the nulls of column name replaced by v, which must fit its type.

    nabi
    let t = table::table!{ "x": [1.0, none, 3.0] };
    let u = try t.fill_null("x", table::Value::Float(0.0)) catch _ { t };
    assert(u.row(1).f64("x") == 0.0);
    
  • pub fn fill_forward(self, name: str) throws (TableError) -> Table§

    Source

    Nulls of column name replaced by the last value above them (still null before the first value).

  • pub fn cast(self, name: str, t: DType) throws (TableError) -> Table§

    Source

    The table with column name converted to t (Column::cast).

    nabi
    let t = table::table!{ "zip": [1234, 501] };
    let u = try t.cast("zip", table::DType::Str) catch _ { t };
    assert(u.row(1).str("zip") == "501");
    
  • pub fn vstack(self, o: Table) throws (TableError) -> Table§

    Source

    This table's rows followed by o's. Columns match by name (any order); both tables need the same names.

    nabi
    let a = table::table!{ "x": [1], "y": ["a"] };
    let b = table::table!{ "y": ["b"], "x": [2] };
    let c = try a.vstack(b) catch _ { a };
    assert(c.nrows() == 2 && c.row(1).str("y") == "b");
    
  • pub fn hstack(self, o: Table) throws (TableError) -> Table§

    Source

    The columns of this table, then o's; both need one row count and distinct names.

  • pub fn concat(ts: List<Table>) throws (TableError) -> Table§

    Source

    The tables' rows, one after another (vstack of each).

  • pub fn unique(self) -> Table§

    Source

    Distinct rows, first occurrence kept, in order.

  • pub fn unique_by(self, names: List<str>) throws (TableError) -> Table§

    Source

    Rows distinct in columns names, first occurrence kept.

Trait implementations 1
  • impl PartialEq for Table

RowSource

pub struct Row

One row of a table, read by column name.

nabi
let t = table::table!{ "a": [1, 2], "b": ["x", "y"] };
let r = t.row(1);
assert(r.get("a") == table::Value::Int(2));
assert(r.str("b") == "y");
  • pub fn index(self) -> usize§

    Source

    The row's position in its table.

  • pub fn get(self, name: str) -> Value§

    Source

    The cell in column name; Null when there is no such column.

  • pub fn at(self, k: usize) -> Value§

    Source

    The cell in column k.

  • pub fn f64(self, name: str) -> f64 | none§

    Source

    Column name as a number, none for null, text or a missing column.

  • pub fn i64(self, name: str) -> i64 | none§

    Source

    Column name as an integer, none for null or another kind.

  • pub fn str(self, name: str) -> str§

    Source

    Column name as text; "" for null or a non-text cell.

  • pub fn values(self) -> List<Value>§

    Source

    Every cell, in column order.