Beginning expressions

Expressions are the heart of Polars, and of Nanuq: almost everything you do to a DataFrame is an expression handed to a method such as select, withColumns, filter, groupBy or sort. This chapter follows the examples of the book's chapter on expressions, on a small table of fruit.

Expressions by example

import nanuq

let fruit = readCsv("data/definitive-guide/fruit.csv")
nbShow fruit
shape: (10, 5)
nameweightcoloris_roundorigin
stri64strboolstr
"Avocado"200"green"false"South America"
"Banana"120"yellow"false"Asia"
"Blueberry"1"blue"false"North America"
"Cantaloupe"2500"orange"true"Africa"
"Cranberry"2"red"false"North America"
"Elderberry"1"black"false"Europe"
"Orange"130"orange"true"Asia"
"Papaya"1000"orange"false"South America"
"Peach"150"orange"true"Asia"
"Watermelon"5000"green"true"Africa"

Selecting columns with expressions

One select can mix a plain column, a regular expression that matches several columns (here color and origin), and a computed column.

nbShow fruit.select(
  col.name,
  col("^.*or.*$"),
  col.weight / 1000,
  col.is_round)
shape: (10, 5)
namecolororiginweightis_round
strstrstrf64bool
"Avocado""green""South America"0.2false
"Banana""yellow""Asia"0.12false
"Blueberry""blue""North America"0.001false
"Cantaloupe""orange""Africa"2.5true
"Cranberry""red""North America"0.002false
"Elderberry""black""Europe"0.001false
"Orange""orange""Asia"0.13true
"Papaya""orange""South America"1.0false
"Peach""orange""Asia"0.15true
"Watermelon""green""Africa"5.0true

Creating new columns with expressions

withColumns adds columns, or replaces them when the name already exists. A literal becomes a column of the same value on every row.

nbShow fruit.withColumns(
  lit(true).alias("is_fruit"),
  col.name.str.endsWith("berry").alias("is_berry"))
shape: (10, 7)
nameweightcoloris_roundoriginis_fruitis_berry
stri64strboolstrboolbool
"Avocado"200"green"false"South America"truefalse
"Banana"120"yellow"false"Asia"truefalse
"Blueberry"1"blue"false"North America"truetrue
"Cantaloupe"2500"orange"true"Africa"truefalse
"Cranberry"2"red"false"North America"truetrue
"Elderberry"1"black"false"Europe"truetrue
"Orange"130"orange"true"Asia"truefalse
"Papaya"1000"orange"false"South America"truefalse
"Peach"150"orange"true"Asia"truefalse
"Watermelon"5000"green"true"Africa"truefalse

Filtering rows with expressions

A predicate is an expression that gives a boolean per row. Predicates combine with and and or (& and | in Python, whose and keyword can't be overloaded).

nbShow fruit.filter(col.weight > 1000 and col.is_round)
shape: (2, 5)
nameweightcoloris_roundorigin
stri64strboolstr
"Cantaloupe"2500"orange"true"Africa"
"Watermelon"5000"green"true"Africa"

Grouping takes expressions too, here the last word of the fruit's origin:

nbShow fruit.groupBy(col.origin.str.split(" ").list.last).agg(
  len(),
  col.weight.mean.alias("average_weight"))
shape: (4, 3)
originlenaverage_weight
stru32f64
"America"4300.75
"Asia"3133.333333
"Africa"23750.0
"Europe"11.0

Sorting rows with expressions

sort takes expressions too, not only column names: here the fruit sorted by the length of their name, longest first.

nbShow fruit.sort([col.name.str.lenBytes], descending = [true])
shape: (10, 5)
nameweightcoloris_roundorigin
stri64strboolstr
"Cantaloupe"2500"orange"true"Africa"
"Elderberry"1"black"false"Europe"
"Watermelon"5000"green"true"Africa"
"Blueberry"1"blue"false"North America"
"Cranberry"2"red"false"North America"
"Avocado"200"green"false"South America"
"Banana"120"yellow"false"Asia"
"Orange"130"orange"true"Asia"
"Papaya"1000"orange"false"South America"
"Peach"150"orange"true"Asia"

The definition of an expression

An expression is a recipe that turns one or more Series into a Series. It can stand for several columns at once: all() below expands to every column of the frame, and name.suffix gives each result a new name.

let ab = newDataFrame:
  a: [1, 2, 3]
  b: [0.4, 0.5, 0.6]
nbShow ab.withColumns((all() * 10).name.suffix("_times_10"))
shape: (3, 4)
aba_times_10b_times_10
i64f64i64f64
10.4104.0
20.5205.0
30.6306.0

Properties of expressions

An expression is a value: you can store it in a variable and use it in different contexts, on different frames, as long as they have the columns it refers to.

let isOrange = (col.color == "orange").alias("is_orange")
nbShow fruit.withColumns(isOrange)
shape: (10, 6)
nameweightcoloris_roundoriginis_orange
stri64strboolstrbool
"Avocado"200"green"false"South America"false
"Banana"120"yellow"false"Asia"false
"Blueberry"1"blue"false"North America"false
"Cantaloupe"2500"orange"true"Africa"true
"Cranberry"2"red"false"North America"false
"Elderberry"1"black"false"Europe"false
"Orange"130"orange"true"Asia"true
"Papaya"1000"orange"false"South America"true
"Peach"150"orange"true"Asia"true
"Watermelon"5000"green"true"Africa"false
nbShow fruit.filter(isOrange)
shape: (4, 5)
nameweightcoloris_roundorigin
stri64strboolstr
"Cantaloupe"2500"orange"true"Africa"
"Orange"130"orange"true"Asia"
"Papaya"1000"orange"false"South America"
"Peach"150"orange"true"Asia"
nbShow fruit.groupBy(isOrange).agg(len())
shape: (2, 2)
is_orangelen
boolu32
false6
true4
let flowers = newDataFrame:
  name: ["Tiger lily", "Blue flag", "African marigold"]
  latin: ["Lilium columbianum", "Iris versicolor", "Tagetes erecta"]
  color: ["orange", "purple", "orange"]
nbShow flowers.filter(isOrange)
shape: (2, 3)
namelatincolor
strstrstr
"Tiger lily""Lilium columbianum""orange"
"African marigold""Tagetes erecta""orange"

Creating expressions

From existing columns

col takes a name, several names, a regular expression or data types.

echo fruit.select(col.color).columns
@["color"]

A name that is not in the frame is an error, raised when the query runs:

try:
  discard fruit.select(col.is_smelly)
except ColumnNotFoundError:
  echo "ColumnNotFoundError"
ColumnNotFoundError
echo fruit.select(col("^.*or.*$")).columns
@["color", "origin"]
echo fruit.select(all()).columns
@["name", "weight", "color", "is_round", "origin"]
echo fruit.select(col(string)).columns
@["name", "color", "origin"]
echo fruit.select(col(bool, int)).columns
@["weight", "is_round"]
echo fruit.select(col("name", "color")).columns
@["name", "color"]

From literal values

A literal is broadcast to the height of the frame.

nbShow fruit.withColumns(lit("Earth").alias("planet"))
shape: (10, 6)
nameweightcoloris_roundoriginplanet
stri64strboolstrstr
"Avocado"200"green"false"South America""Earth"
"Banana"120"yellow"false"Asia""Earth"
"Blueberry"1"blue"false"North America""Earth"
"Cantaloupe"2500"orange"true"Africa""Earth"
"Cranberry"2"red"false"North America""Earth"
"Elderberry"1"black"false"Europe""Earth"
"Orange"130"orange"true"Asia""Earth"
"Papaya"1000"orange"false"South America""Earth"
"Peach"150"orange"true"Asia""Earth"
"Watermelon"5000"green"true"Africa""Earth"

Renaming expressions

alias names the result of an expression.

let df = newDataFrame:
  text: ["value"]
  "An integer": [5040]
  BOOLEAN: [true]
nbShow df.select(col("An integer").alias("int"))
shape: (1, 1)
int
i64
5040

Expressions are idiomatic

The book closes the chapter by contrasting expressions with Series computed up front: expressions refer to columns by name, so they also work in a lazy query, where the data is not there yet.

nbShow fruit.lazy
  .filter(col.weight > 1000 and col.is_round)
  .withColumns(col.name.str.endsWith("berry").alias("is_berry"))
  .collect
shape: (2, 6)
nameweightcoloris_roundoriginis_berry
stri64strboolstrbool
"Cantaloupe"2500"orange"true"Africa"false
"Watermelon"5000"green"true"Africa"false
import nanuqbook
bookInit()

nbText: "# Beginning expressions"

attribution definitiveGuide(7, "Beginning Expressions",
  "Data is read from `data/definitive-guide/`. Left out: `meta.has_multiple_outputs`, top-level " &
  "`pl.select`, list literals, `repeat`/`zeros`/`ones`, the range functions, the `name` " &
  "methods other than `suffix`/`prefix`, and Series arithmetic (`fruit[\"weight\"] > 1000`).")

nbText: """
Expressions are the heart of Polars, and of Nanuq: almost everything you do to a
DataFrame is an expression handed to a method such as `select`, `withColumns`, `filter`,
`groupBy` or `sort`. This chapter follows the examples of the book's chapter on
expressions, on a small table of fruit.

## Expressions by example
"""

tabs """
import polars as pl

fruit = pl.read_csv("data/definitive-guide/fruit.csv")
fruit
""":
  nbCode:
    import nanuq

    let fruit = readCsv("data/definitive-guide/fruit.csv")
    nbShow fruit

nbText: """
### Selecting columns with expressions

One `select` can mix a plain column, a regular expression that matches several columns
(here `color` and `origin`), and a computed column.
"""

tabs """
fruit.select(
    pl.col("name"),
    pl.col("^.*or.*$"),
    pl.col("weight") / 1000,
    "is_round",
)
""":
  nbCode:
    nbShow fruit.select(
      col.name,
      col("^.*or.*$"),
      col.weight / 1000,
      col.is_round)

nanuq """
`col.name` is the 0.3 shorthand for `col("name")`, like Python's `pl.col.name`. Python also
accepts a bare string (`"is_round"`) where it expects an expression; Nim's `select` takes
expressions only. And `/` is true division, so the integer weights become floats, as in
Python.
"""

nbText: """
### Creating new columns with expressions

`withColumns` adds columns, or replaces them when the name already exists. A literal
becomes a column of the same value on every row.
"""

tabs """
fruit.with_columns(
    pl.lit(True).alias("is_fruit"),
    is_berry=pl.col("name").str.ends_with("berry"),
)
""":
  nbCode:
    nbShow fruit.withColumns(
      lit(true).alias("is_fruit"),
      col.name.str.endsWith("berry").alias("is_berry"))

nanuq """
Python's keyword arguments (`is_berry=...`) name a column; in Nim the name always comes
from `alias`.
"""

nbText: """
### Filtering rows with expressions

A predicate is an expression that gives a boolean per row. Predicates combine with `and`
and `or` (`&` and `|` in Python, whose `and` keyword can't be overloaded).
"""

tabs """
fruit.filter(
    (pl.col("weight") > 1000)
    & pl.col("is_round")
)
""":
  nbCode:
    nbShow fruit.filter(col.weight > 1000 and col.is_round)

nbText: "Grouping takes expressions too, here the last word of the fruit's origin:"

tabs """
fruit.group_by(pl.col("origin").str.split(" ").list.last()).agg(
    pl.len(),
    average_weight=pl.col("weight").mean()
)
""":
  nbCode:
    nbShow fruit.groupBy(col.origin.str.split(" ").list.last).agg(
      len(),
      col.weight.mean.alias("average_weight"))

nbText: """
### Sorting rows with expressions

`sort` takes expressions too, not only column names: here the fruit sorted by the length
of their name, longest first.
"""

tabs """
fruit.sort(
    pl.col("name").str.len_bytes(),
    descending=True,
)
""":
  nbCode:
    nbShow fruit.sort([col.name.str.lenBytes], descending = [true])

nbText: """
## The definition of an expression

An expression is a recipe that turns one or more Series into a Series. It can stand for
several columns at once: `all()` below expands to every column of the frame, and
`name.suffix` gives each result a new name.
"""

tabs """
(
    pl.DataFrame({"a": [1, 2, 3], "b": [0.4, 0.5, 0.6]}).with_columns(
        pl.all().mul(10).name.suffix("_times_10")
    )
)
""":
  nbCode:
    let ab = newDataFrame:
      a: [1, 2, 3]
      b: [0.4, 0.5, 0.6]
    nbShow ab.withColumns((all() * 10).name.suffix("_times_10"))

nanuq """
Python's operators have method twins (`mul` for `*`); Nim just uses the operators. The
frame is built with the block syntax of `newDataFrame`, new in Nanuq 0.3.
"""

nbText: """
### Properties of expressions

An expression is a value: you can store it in a variable and use it in different
contexts, on different frames, as long as they have the columns it refers to.
"""

tabs """
is_orange = (pl.col("color") == "orange").alias("is_orange")

fruit.with_columns(is_orange)
""":
  nbCode:
    let isOrange = (col.color == "orange").alias("is_orange")
    nbShow fruit.withColumns(isOrange)

tabs """
fruit.filter(is_orange)
""":
  nbCode:
    nbShow fruit.filter(isOrange)

tabs """
fruit.group_by(is_orange).len()
""":
  nbCode:
    nbShow fruit.groupBy(isOrange).agg(len())

nanuq """
Nanuq's group-by has no `len()` shortcut: `agg(len())` counts the rows of each group.
"""

tabs """
flowers = pl.DataFrame(
    {
        "name": ["Tiger lily", "Blue flag", "African marigold"],
        "latin": ["Lilium columbianum", "Iris versicolor", "Tagetes erecta"],
        "color": ["orange", "purple", "orange"],
    }
)

flowers.filter(is_orange)
""":
  nbCode:
    let flowers = newDataFrame:
      name: ["Tiger lily", "Blue flag", "African marigold"]
      latin: ["Lilium columbianum", "Iris versicolor", "Tagetes erecta"]
      color: ["orange", "purple", "orange"]
    nbShow flowers.filter(isOrange)

nbText: """
## Creating expressions

### From existing columns

`col` takes a name, several names, a regular expression or data types.
"""

tabs """
fruit.select(pl.col("color")).columns
""":
  nbCode:
    echo fruit.select(col.color).columns

nbText: """
A name that is not in the frame is an error, raised when the query runs:
"""

tabs """
try:
    fruit.select(pl.col("is_smelly"))
except pl.exceptions.ColumnNotFoundError as e:
    print(type(e).__name__)
""":
  nbCode:
    try:
      discard fruit.select(col.is_smelly)
    except ColumnNotFoundError:
      echo "ColumnNotFoundError"

nanuq """
Since 0.3 Nanuq raises the same error types as Python Polars, all subtypes of
`PolarsError`. The book shows the error by leaving the cell commented out; here both sides
catch it.
"""

tabs """
fruit.select(pl.col("^.*or.*$")).columns
""":
  nbCode:
    echo fruit.select(col("^.*or.*$")).columns

tabs """
fruit.select(pl.all()).columns
""":
  nbCode:
    echo fruit.select(all()).columns

tabs """
fruit.select(pl.col(pl.String)).columns
""":
  nbCode:
    echo fruit.select(col(string)).columns

tabs """
fruit.select(pl.col(pl.Boolean, pl.Int64)).columns
""":
  nbCode:
    echo fruit.select(col(bool, int)).columns

tabs """
fruit.select(pl.col(["name", "color"])).columns
""":
  nbCode:
    echo fruit.select(col("name", "color")).columns

nanuq """
Data types are Nim types in `col`: `string` for `pl.String`, `bool` for `pl.Boolean`, `int`
for `pl.Int64`.
"""

nbText: """
### From literal values

A literal is broadcast to the height of the frame.
"""

tabs """
fruit.with_columns(planet=pl.lit("Earth"))
""":
  nbCode:
    nbShow fruit.withColumns(lit("Earth").alias("planet"))

nanuq """
Left out from this section: `pl.select` without a frame, list literals, `repeat`, `zeros`
and `ones`, and in the next sections the range functions (`int_range`, `date_range`,
`datetime_ranges`) and the `name` methods `to_uppercase`, `to_lowercase` and `map`. Nanuq
does not have them yet.
"""

nbText: """
## Renaming expressions

`alias` names the result of an expression.
"""

tabs """
df = pl.DataFrame({"text": "value", "An integer": 5040, "BOOLEAN": True})
df.select(pl.col("An integer").alias("int"))
""":
  nbCode:
    let df = newDataFrame:
      text: ["value"]
      "An integer": [5040]
      BOOLEAN: [true]
    nbShow df.select(col("An integer").alias("int"))

nbText: """
## Expressions are idiomatic

The book closes the chapter by contrasting expressions with Series computed up front:
expressions refer to columns by name, so they also work in a lazy query, where the data is
not there yet.
"""

tabs """
(
    fruit.lazy()
    .filter((pl.col("weight") > 1000) & pl.col("is_round"))
    .with_columns(is_berry=pl.col("name").str.ends_with("berry"))
    .collect()
)
""":
  nbCode:
    nbShow fruit.lazy
      .filter(col.weight > 1000 and col.is_round)
      .withColumns(col.name.str.endsWith("berry").alias("is_berry"))
      .collect

bookSave()
Made with nimib and nimibook. Blocks marked "ran" were executed when this page was built.