Expressions

Polars has a powerful concept called expressions (Expr type). Polars expressions can be used in various contexts and essentially perform the function Fn(Series) -> Series. An Expr takes a Series as input and outputs a Series. Therefore, Expr can be chained together.

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

The above snippet represents selecting the column "foo", sorting it, and then taking the first two values of the sorted output. The power of expressions lies in the fact that each expression produces a new expression, and they can be chained, stored in variables, or passed as parameters. You can run expressions through Polars' execution context. 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 independent Polars expression can run independently without needing the results of any other expression or interacting with other expressions. Therefore, Polars may assign expressions to different threads or processors to execute simultaneously. Polars expressions are highly parallel. Understanding Polars expressions is a key step in learning Polars.

Contexts

Functions that can accept expressions are called contexts, and they include the following three types:

MeaningCode
Selectdf.select([..])
Group aggregationdf.groupby(..).agg([..])
Horizontal stacking (hstack) or adding columnsdf.with_columns([..])