表达式

Polars has a powerful concept called expressions (Expr types). Polars expressions can be used in a variety of contexts, essentially executing Fn(Series) -> Series. Expr takes the Series as input and the Series as the output. So Expr can be chained.

#![allow(unused)]
fn main() {
col("foo").sort().head(2)
}

The above snippet represents selecting the column "foo", then sorting this column, and then taking the first two values of the sort output. The power of expressions is that each expression produces a new expression, and they can be chained or saved into a variable or passed as an argument. You can run expressions through the execution context of polars. Here we run two expressions in the select context:

#![allow(unused)]
fn main() {
df.lazy()
  .select([
  col("foo").sort(Default::default()).head(None),
  col("bar").filter(col("foo").eq(lit(1))).sum(),
])
.collect()?;
}

Each individual Polars expression can be run independently without the result of any other expression or without interacting with other expressions. As a result, Polars may assign expressions to different threads or processors to execute at the same time. Polars expressions are highly parallel. Understanding Polars expressions is a crucial step in learning Polars.

Context

Functions that accept Expressions are called contexts, and there are three types of functions:

MeaningCode
Select contextdf.select([..])
group/agg contextdf.groupby(..).agg([..])
Stack horizontally (hstack) or add columnsdf.with_columns([..])