packages

table::expr

On this page

Column expressions: a small tree built with the dot operators (col("x") .* 2 .+ col("y"), col("a") .> 3 .&& col("b") .== "k") and evaluated column at a time against a table.

Structs

ExprSource

pub struct Expr

A computed column: names columns with col, scalars with lit (or a plain number, string, bool or date on either side of an operator), and combines them with the dot operators. Nulls propagate; .&& / .|| use three-valued logic (false .&& null is false).

OperatorsResult
.+ .- .* .%i64 for two i64, else f64
./ .**f64
.== .!= .< .<= .> .>=bool
`.&& .
prefix -the operand's type
nabi
use table::{col, lit};
let t = table::table!{ "x": [1.0, 2.0, 3.0], "y": [4.0, 5.0, 6.0] };
let e = (col("x") .+ col("y")) ./ 2 .- lit(1.0);
let u = try t.with_column("m", e) catch _ { t };
assert(u.row(0).f64("m") == 1.5);
  • pub fn is_null(self) -> Expr§

    Source

    true where the value is null.

    nabi
    let t = table::table!{ "x": [1, none] };
    assert((try t.filter(table::col("x").is_null()) catch _ { t }).nrows() == 1);
    
  • pub fn is_not_null(self) -> Expr§

    Source

    true where there is a value.

  • pub fn fill_null<T: IntoExpr>(self, v: T) -> Expr§

    Source

    Nulls replaced by v.

  • pub fn cast(self, t: DType) -> Expr§

    Source

    The values as type t (Column::cast).

  • pub fn is_in(self, vs: List<Value>) -> Expr§

    Source

    true where the value is one of vs.

    nabi
    use table::Value;
    let t = table::table!{ "c": ["a", "b", "c"] };
    let u = try t.filter(table::col("c").is_in([Value::Str("a"), Value::Str("c")])) catch _ { t };
    assert(u.nrows() == 2);
    
  • pub fn abs(self) -> Expr§

    Source

    |x|, keeping the type.

  • pub fn sqrt(self) -> Expr§

    Source

    Square root, as f64.

  • pub fn exp(self) -> Expr§

    Source

    e to the value, as f64.

  • pub fn ln(self) -> Expr§

    Source

    Natural logarithm.

  • pub fn log10(self) -> Expr§

    Source

    Base-10 logarithm, as f64.

  • pub fn round(self) -> Expr§

    Source

    Half away from zero, as f64::round.

  • pub fn floor(self) -> Expr§

    Source

    Round down (an i64 column stays as it is).

  • pub fn ceil(self) -> Expr§

    Source

    Round up (an i64 column stays as it is).

  • pub fn year(self) -> Expr§

    Source

    The year of a date or timestamp, as i64.

    nabi
    let t = table::table!{ "d": [date"2024-03-01", date"1999-12-31"] };
    let u = try t.with_column("y", table::col("d").year()) catch _ { t };
    assert(u.row(1).i64("y") == 1999);
    
  • pub fn month(self) -> Expr§

    Source

    The month of a date or timestamp, 1 to 12.

  • pub fn day(self) -> Expr§

    Source

    The day of the month, 1 to 31.

  • pub fn weekday(self) -> Expr§

    Source

    Monday 0 to Sunday 6.

  • pub fn len(self) -> Expr§

    Source

    The length of a string in code points.

  • pub fn upper(self) -> Expr§

    Source

    The text in upper case.

  • pub fn lower(self) -> Expr§

    Source

    The text in lower case.

  • pub fn contains(self, s: str) -> Expr§

    Source

    true where the text contains s.

  • pub fn starts_with(self, s: str) -> Expr§

    Source

    true where the text starts with s.

  • pub fn ends_with(self, s: str) -> Expr§

    Source

    true where the text ends with s.

  • pub fn cum_sum(self) -> Expr§

    Source

    Running sum down the column; nulls stay null and are skipped.

    nabi
    let t = table::table!{ "x": [1, 2, 3] };
    let u = try t.with_column("s", table::col("x").cum_sum()) catch _ { t };
    assert(u.row(2).i64("s") == 6);
    
  • pub fn cum_prod(self) -> Expr§

    Source

    Running product; nulls stay null and are skipped.

  • pub fn cum_min(self) -> Expr§

    Source

    Running minimum; nulls stay null and are skipped.

  • pub fn cum_max(self) -> Expr§

    Source

    Running maximum; nulls stay null and are skipped.

  • pub fn cum_count(self) -> Expr§

    Source

    How many non-null values so far.

  • pub fn rolling_sum(self, window: usize) -> Expr§

    Source

    Sum of the last window rows (this one included); null until window values have been seen. Neumaier-compensated running sum, O(n).

    nabi
    let t = table::table!{ "x": [1.0, 2.0, 3.0, 4.0] };
    let u = try t.with_column("r", table::col("x").rolling_mean(2)) catch _ { t };
    assert(u.row(3).f64("r") == 3.5 && u.row(0).f64("r") is none);
    
  • pub fn rolling_mean(self, window: usize) -> Expr§

    Source

    Mean of the last window rows (compensated running sum).

  • pub fn rolling_min(self, window: usize) -> Expr§

    Source

    Minimum over the window, monotonic deque, O(n).

  • pub fn rolling_max(self, window: usize) -> Expr§

    Source

    Maximum over the window, monotonic deque, O(n).

  • pub fn rolling_std(self, window: usize) -> Expr§

    Source

    Sample standard deviation over the window (Welford update with removal, O(n)).

  • pub fn rolling(self, stat: str, window: usize, min_periods: usize) -> Expr§

    Source

    A rolling statistic that starts once min_periods values of the window are present (nulls do not count).

  • pub fn shift(self, k: i64) -> Expr§

    Source

    The column moved down by k rows (up for a negative k), nulls filling the gap.

  • pub fn diff(self, k: i64) -> Expr§

    Source

    x - x.shift(k).

  • pub fn map(self, f: fn(Value) -> Value) -> Expr§

    Source

    f of each cell (nulls included), as one column; the result type comes from the cells (Column::from_values).

    nabi
    use table::Value;
    let t = table::table!{ "s": ["a", "bb"] };
    let e = table::col("s").map(|v| Value::Int((v.as_str() ?? "").len() as i64));
    assert((try t.with_column("n", e) catch _ { t }).row(1).i64("n") == 2);
    
  • pub fn map_f64(self, f: fn(f64) -> f64) -> Expr§

    Source

    f of each non-null number, as f64.

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

    Source

    The names of the columns this expression reads.

  • pub fn eval(self, t: Table) throws (TableError) -> Column§

    Source

    The expression's values over t, as a column of t.nrows() rows.

    nabi
    let t = table::table!{ "x": [1, 2] };
    let c = try (table::col("x") .* 10).eval(t) catch _ { table::Column::i64s([]) };
    assert(c.get(1) == table::Value::Int(20));
    
Trait implementations 8
  • impl Clone for Expr
  • impl IntoExpr for Expr
  • impl Display for Expr
  • impl Debug for Expr
  • impl ops::Neg for Expr
  • impl ops::Not for Expr
  • impl<R: IntoExpr> ops::DotLogicalAnd<R> for Expr
  • impl<R: IntoExpr> ops::DotLogicalOr<R> for Expr

WhenSource

pub struct When

A when(..) waiting for its then.

ThenSource

pub struct Then

A when(..).then(..) waiting for its otherwise.

  • pub fn otherwise<T: IntoExpr>(self, v: T) -> Expr§

    Source

    The value elsewhere (null condition included); the finished expression.

Traits

IntoExprSource

pub trait IntoExpr

What can stand on either side of an expression operator: an Expr, a number, string, bool, date or Value.

Functions

colSource

pub fn col(name: str) -> Expr

Column name of the table the expression runs against.

nabi
let e = table::col("price") .* 1.1;
assert(f"{e}" == "(col(\"price\") .* 1.1)");

litSource

pub fn lit<T: IntoExpr>(v: T) -> Expr

A scalar, the same in every row.

nabi
let e = table::lit(2.5);
assert(f"{e}" == "2.5");

whenSource

pub fn when<C: IntoExpr>(cond: C) -> When

then where cond is true, else otherwise; start with when.

nabi
use table::{col, when};
let t = table::table!{ "x": [-1.0, 2.0] };
let u = try t.with_column("pos", when(col("x") .> 0).then(col("x")).otherwise(0.0)) catch _ { t };
assert(u.row(0).f64("pos") == 0.0);