Combining expressions
Expressions get interesting when they are combined: with arithmetic, comparisons, Boolean logic, bitwise operators, and with functions that take several expressions at once. This chapter goes through each kind, again on the table of fruit.
import nanuq
let fruit = readCsv("data/definitive-guide/fruit.csv")
nbShow fruit.filter(col.is_round and col.weight > 1000)
| name | weight | color | is_round | origin |
|---|---|---|---|---|
| str | i64 | str | bool | str |
| "Cantaloupe" | 2500 | "orange" | true | "Africa" |
| "Watermelon" | 5000 | "green" | true | "Africa" |
import polars as pl
fruit = pl.read_csv("data/definitive-guide/fruit.csv")
fruit.filter(pl.col("is_round") & (pl.col("weight") > 1000))| name | weight | color | is_round | origin |
|---|---|---|---|---|
| str | i64 | str | bool | str |
| "Cantaloupe" | 2500 | "orange" | true | "Africa" |
| "Watermelon" | 5000 | "green" | true | "Africa" |
Inline operators versus methods
Each operator is also a method: * is mul. In Nim an operator already is a proc, so
col.i * col.j is the call `*`(col.i, col.j); Nanuq has the method names too, for
code ported from Python.
let ij = newDataFrame:
i: [6.0, 0, 2, 2.5]
j: [7.0, 1, 2, 3]
nbShow ij.withColumns(
(col.i * col.j).alias("*"),
col.i.mul(col.j).alias("Expr.mul()"))
| i | j | * | Expr.mul() |
|---|---|---|---|
| f64 | f64 | f64 | f64 |
| 6.0 | 7.0 | 42.0 | 42.0 |
| 0.0 | 1.0 | 0.0 | 0.0 |
| 2.0 | 2.0 | 4.0 | 4.0 |
| 2.5 | 3.0 | 7.5 | 7.5 |
pl.DataFrame({"i": [6.0, 0, 2, 2.5], "j": [7.0, 1, 2, 3]}).with_columns(
(pl.col("i") * pl.col("j")).alias("*"),
pl.col("i").mul(pl.col("j")).alias("Expr.mul()"),
)| i | j | * | Expr.mul() |
|---|---|---|---|
| f64 | f64 | f64 | f64 |
| 6.0 | 7.0 | 42.0 | 42.0 |
| 0.0 | 1.0 | 0.0 | 0.0 |
| 2.0 | 2.0 | 4.0 | 4.0 |
| 2.5 | 3.0 | 7.5 | 7.5 |
Arithmetic operations
Dividing the weight in grams by 1000 gives kilograms:
nbShow fruit.select(col.name, col.weight / 1000)
| name | weight |
|---|---|
| str | f64 |
| "Avocado" | 0.2 |
| "Banana" | 0.12 |
| "Blueberry" | 0.001 |
| "Cantaloupe" | 2.5 |
| "Cranberry" | 0.002 |
| "Elderberry" | 0.001 |
| "Orange" | 0.13 |
| "Papaya" | 1.0 |
| "Peach" | 0.15 |
| "Watermelon" | 5.0 |
fruit.select(pl.col("name"), (pl.col("weight") / 1000))| name | weight |
|---|---|
| str | f64 |
| "Avocado" | 0.2 |
| "Banana" | 0.12 |
| "Blueberry" | 0.001 |
| "Cantaloupe" | 2.5 |
| "Cranberry" | 0.002 |
| "Elderberry" | 0.001 |
| "Orange" | 0.13 |
| "Papaya" | 1.0 |
| "Peach" | 0.15 |
| "Watermelon" | 5.0 |
All the arithmetic operators side by side, with two decimals in the output. Python's floor
division // is Nim's div, its power ** is ^, and its modulo % is mod.
setFloatPrecision(2)
let arith = newDataFrame:
i: [0.0, 2, 2, -2, -2]
j: [1, 2, 3, 4, -5]
nbShow arith.withColumns(
(col.i + col.j).alias("i + j"),
(col.i - col.j).alias("i - j"),
(col.i * col.j).alias("i * j"),
(col.i / col.j).alias("i / j"),
(col.i div col.j).alias("i // j"),
(col.i ^ col.j).alias("i ** j"),
(col.j mod 2).alias("j % 2"),
col.i.dot(col.j).alias("i β
j"))
| i | j | i + j | i - j | i * j | i / j | i // j | i ** j | j % 2 | i β j |
|---|---|---|---|---|---|---|---|---|---|
| f64 | i64 | f64 | f64 | f64 | f64 | f64 | f64 | i64 | f64 |
| 0.00 | 1 | 1.00 | -1.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1 | 12.00 |
| 2.00 | 2 | 4.00 | 0.00 | 4.00 | 1.00 | 1.00 | 4.00 | 0 | 12.00 |
| 2.00 | 3 | 5.00 | -1.00 | 6.00 | 0.67 | 0.00 | 8.00 | 1 | 12.00 |
| -2.00 | 4 | 2.00 | -6.00 | -8.00 | -0.50 | -1.00 | 16.00 | 0 | 12.00 |
| -2.00 | -5 | -7.00 | 3.00 | 10.00 | 0.40 | 0.00 | -0.03 | 1 | 12.00 |
pl.Config(float_precision=2)
(
pl.DataFrame({"i": [0.0, 2, 2, -2, -2], "j": [1, 2, 3, 4, -5]}).with_columns(
(pl.col("i") + pl.col("j")).alias("i + j"),
(pl.col("i") - pl.col("j")).alias("i - j"),
(pl.col("i") * pl.col("j")).alias("i * j"),
(pl.col("i") / pl.col("j")).alias("i / j"),
(pl.col("i") // pl.col("j")).alias("i // j"),
(pl.col("i") ** pl.col("j")).alias("i ** j"),
(pl.col("j") % 2).alias("j % 2"),
pl.col("i").dot(pl.col("j")).alias("i β
j"),
)
)| i | j | i + j | i - j | i * j | i / j | i // j | i ** j | j % 2 | i β j |
|---|---|---|---|---|---|---|---|---|---|
| f64 | i64 | f64 | f64 | f64 | f64 | f64 | f64 | i64 | f64 |
| 0.00 | 1 | 1.00 | -1.00 | 0.00 | 0.00 | 0.00 | 0.00 | 1 | 12.00 |
| 2.00 | 2 | 4.00 | 0.00 | 4.00 | 1.00 | 1.00 | 4.00 | 0 | 12.00 |
| 2.00 | 3 | 5.00 | -1.00 | 6.00 | 0.67 | 0.00 | 8.00 | 1 | 12.00 |
| -2.00 | 4 | 2.00 | -6.00 | -8.00 | -0.50 | -1.00 | 16.00 | 0 | 12.00 |
| -2.00 | -5 | -7.00 | 3.00 | 10.00 | 0.40 | 0.00 | -0.03 | 1 | 12.00 |
pl.Config.set_float_precision()<class 'polars.config.Config'>
Comparison operations
Strings compare alphabetically. select without a frame evaluates expressions that need
no input columns:
nbShow select(lit("a") > lit("b"))
| literal |
|---|
| bool |
| false |
pl.select(pl.lit("a") > pl.lit("b"))| literal |
|---|
| bool |
| false |
nbShow fruit.select(col.name, col.weight).filter(col.weight >= 1000)
| name | weight |
|---|---|
| str | i64 |
| "Cantaloupe" | 2500 |
| "Papaya" | 1000 |
| "Watermelon" | 5000 |
fruit.select(
pl.col("name"),
pl.col("weight"),
).filter(pl.col("weight") >= 1000)| name | weight |
|---|---|
| str | i64 |
| "Cantaloupe" | 2500 |
| "Papaya" | 1000 |
| "Watermelon" | 5000 |
Python chains comparisons (3 < x < 5), but not on expressions: those need two
comparisons joined with and, or isBetween. Nim has no chained comparisons at all, so
the expression version reads like the plain one.
let x = 4
echo select(lit(3) < lit(x) and lit(x) < lit(5)).toSeries.item(bool)
true
x = 4
pl.select((pl.lit(3) < pl.lit(x)) & (pl.lit(x) < pl.lit(5))).item()True
echo select(lit(x).isBetween(3, 5)).toSeries.item(bool)
true
pl.select(pl.lit(x).is_between(3, 5)).item()True
Every comparison operator, and two ways of saying "close enough" or "in a range":
let ab = newDataFrame:
a: [-273.15, 0, 42, 100]
b: [1.4142, 2.7183, 42, 3.1415]
nbShow ab.withColumns(
(col.a == col.b).alias("a == b"),
(col.a <= col.b).alias("a <= b"),
(all() > 0).name.suffix(" > 0"),
((col.b - lit(2).sqrt).abs < 1e-3).alias("b β β2"),
(1 < col.b and col.b < 3).alias("1 < b < 3"))
| a | b | a == b | a <= b | a > 0 | b > 0 | b β β2 | 1 < b < 3 |
|---|---|---|---|---|---|---|---|
| f64 | f64 | bool | bool | bool | bool | bool | bool |
| -273.15 | 1.4142 | false | true | false | true | true | true |
| 0.0 | 2.7183 | false | true | false | true | false | true |
| 42.0 | 42.0 | true | true | true | true | false | false |
| 100.0 | 3.1415 | false | false | true | true | false | false |
pl.DataFrame(
{"a": [-273.15, 0, 42, 100], "b": [1.4142, 2.7183, 42, 3.1415]}
).with_columns(
(pl.col("a") == pl.col("b")).alias("a == b"),
(pl.col("a") <= pl.col("b")).alias("a <= b"),
(pl.all() > 0).name.suffix(" > 0"),
((pl.col("b") - pl.lit(2).sqrt()).abs() < 1e-3).alias("b β β2"),
((1 < pl.col("b")) & (pl.col("b") < 3)).alias("1 < b < 3"),
)| a | b | a == b | a <= b | a > 0 | b > 0 | b β β2 | 1 < b < 3 |
|---|---|---|---|---|---|---|---|
| f64 | f64 | bool | bool | bool | bool | bool | bool |
| -273.15 | 1.4142 | false | true | false | true | true | true |
| 0.0 | 2.7183 | false | true | false | true | false | true |
| 42.0 | 42.0 | true | true | true | true | false | false |
| 100.0 | 3.1415 | false | false | true | true | false | false |
Values of different types can be compared when Polars knows how to line them up: Booleans
with numbers, a time with a time, a datetime with a date. A string and a number cannot,
so the number is cast to a string first (and then "5" < "3" is false).
import std/times
nbShow select(
(lit(true) > 0).alias("bool_num"),
(lit(timeOfDay(23, 58)) > lit(timeOfDay(0, 0))).alias("time_time"),
(lit(dateTime(1969, mJul, 21, 2, 56, zone = utc())) <
lit(date(1976, 7, 20))).alias("datetime_date"),
(lit("5") < lit(3).cast(String)).alias("str_num"),
(lit(dateTime(1999, mJan, 1, zone = utc())).dt.time != lit(timeOfDay(0, 0))).alias("datetime_time"),
).transpose(includeHeader = true, headerName = "comparison", columnNames = ["allowed"])
| comparison | allowed |
|---|---|
| str | bool |
| "bool_num" | true |
| "time_time" | true |
| "datetime_date" | true |
| "str_num" | false |
| "datetime_time" | false |
from datetime import date, datetime, time
pl.select(
bool_num=pl.lit(True) > 0,
time_time=pl.lit(time(23, 58)) > pl.lit(time(0, 0)),
datetime_date=pl.lit(datetime(1969, 7, 21, 2, 56)) < pl.lit(date(1976, 7, 20)),
str_num=pl.lit("5") < pl.lit(3).cast(pl.String),
datetime_time=pl.lit(datetime(1999, 1, 1)).dt.time() != pl.lit(time(0, 0)),
).transpose(include_header=True, header_name="comparison", column_names=["allowed"])| comparison | allowed |
|---|---|
| str | bool |
| "bool_num" | true |
| "time_time" | true |
| "datetime_date" | true |
| "str_num" | false |
| "datetime_time" | false |
Boolean algebra operations
With x = 7, p (3 < x) is true and q (x < 5) is false, so p and q is false:
block:
let x = 7
let p = lit(3) < lit(x)
let q = lit(x) < lit(5)
echo select(p and q).toSeries.item(bool)
false
x = 7
p = pl.lit(3) < pl.lit(x)
q = pl.lit(x) < pl.lit(5)
pl.select(p & q).item()False
The whole truth table, including nand (β) and nor (β), which are the negations of and and or:
let pq = newDataFrame:
p: [true, true, false, false]
q: [true, false, true, false]
nbShow pq.withColumns(
(col.p and col.q).alias("p & q"),
(col.p or col.q).alias("p | q"),
(not col.p).alias("~p"),
(col.p xor col.q).alias("p ^ q"),
(not (col.p and col.q)).alias("p β q"),
(not (col.p or col.q)).alias("p β q"))
| p | q | p & q | p | q | ~p | p ^ q | p β q | p β q |
|---|---|---|---|---|---|---|---|
| bool | bool | bool | bool | bool | bool | bool | bool |
| true | true | true | true | false | false | false | false |
| true | false | false | true | false | true | true | false |
| false | true | false | true | true | true | true | false |
| false | false | false | false | true | false | true | true |
pl.DataFrame(
{"p": [True, True, False, False], "q": [True, False, True, False]}
).with_columns(
(pl.col("p") & pl.col("q")).alias("p & q"),
(pl.col("p") | pl.col("q")).alias("p | q"),
(~pl.col("p")).alias("~p"),
(pl.col("p") ^ pl.col("q")).alias("p ^ q"),
(~(pl.col("p") & pl.col("q"))).alias("p β q"),
((pl.col("p").or_(pl.col("q"))).not_()).alias("p β q"),
)| p | q | p & q | p | q | ~p | p ^ q | p β q | p β q |
|---|---|---|---|---|---|---|---|
| bool | bool | bool | bool | bool | bool | bool | bool |
| true | true | true | true | false | false | false | false |
| true | false | false | true | false | true | true | false |
| false | true | false | true | true | true | true | false |
| false | false | false | false | true | false | true | true |
Bitwise operations
On integers the same operators work bit by bit: 10 is 00001010 and 34 is 00100010,
so 10 or 34 is 00101010, which is 42.
echo select(lit(10) or lit(34)).toSeries.item(int)
42
pl.select(pl.lit(10) | pl.lit(34)).item()42
let bits = newDataFrame(
series("x", [1'u8, 1, 0, 0, 7, 10]),
series("y", [1'u8, 0, 1, 0, 2, 34]),
).withColumns(
(col.x and col.y).alias("x & y"),
(col.x or col.y).alias("x | y"),
(not col.x).alias("~x"),
(col.x xor col.y).alias("x ^ y"))
nbShow bits
| x | y | x & y | x | y | ~x | x ^ y |
|---|---|---|---|---|---|
| u8 | u8 | u8 | u8 | u8 | u8 |
| 1 | 1 | 1 | 1 | 254 | 0 |
| 1 | 0 | 0 | 1 | 254 | 1 |
| 0 | 1 | 0 | 1 | 255 | 1 |
| 0 | 0 | 0 | 0 | 255 | 0 |
| 7 | 2 | 2 | 7 | 248 | 5 |
| 10 | 34 | 2 | 42 | 245 | 40 |
bits = pl.DataFrame(
{"x": [1, 1, 0, 0, 7, 10], "y": [1, 0, 1, 0, 2, 34]},
schema={"x": pl.UInt8, "y": pl.UInt8},
).with_columns(
(pl.col("x") & pl.col("y")).alias("x & y"),
(pl.col("x") | pl.col("y")).alias("x | y"),
(~pl.col("x")).alias("~x"),
(pl.col("x") ^ pl.col("y")).alias("x ^ y"),
)
bits| x | y | x & y | x | y | ~x | x ^ y |
|---|---|---|---|---|---|
| u8 | u8 | u8 | u8 | u8 | u8 |
| 1 | 1 | 1 | 1 | 254 | 0 |
| 1 | 0 | 0 | 1 | 254 | 1 |
| 0 | 1 | 0 | 1 | 255 | 1 |
| 0 | 0 | 0 | 0 | 255 | 0 |
| 7 | 2 | 2 | 7 | 248 | 5 |
| 10 | 34 | 2 | 42 | 245 | 40 |
To see the bits, each value goes through a formatting function. In Python that is
"{0:08b}".format; in Nim it is a proc from std/strutils, called by mapElements:
import std/strutils
nbShow bits.select(all().mapElements(proc (v: uint8): string = v.int.toBin(8)))
| x | y | x & y | x | y | ~x | x ^ y |
|---|---|---|---|---|---|
| str | str | str | str | str | str |
| "00000001" | "00000001" | "00000001" | "00000001" | "11111110" | "00000000" |
| "00000001" | "00000000" | "00000000" | "00000001" | "11111110" | "00000001" |
| "00000000" | "00000001" | "00000000" | "00000001" | "11111111" | "00000001" |
| "00000000" | "00000000" | "00000000" | "00000000" | "11111111" | "00000000" |
| "00000111" | "00000010" | "00000010" | "00000111" | "11111000" | "00000101" |
| "00001010" | "00100010" | "00000010" | "00101010" | "11110101" | "00101000" |
bits.select(pl.all().map_elements("{0:08b}".format, return_dtype=pl.String))| x | y | x & y | x | y | ~x | x ^ y |
|---|---|---|---|---|---|
| str | str | str | str | str | str |
| "00000001" | "00000001" | "00000001" | "00000001" | "11111110" | "00000000" |
| "00000001" | "00000000" | "00000000" | "00000001" | "11111110" | "00000001" |
| "00000000" | "00000001" | "00000000" | "00000001" | "11111111" | "00000001" |
| "00000000" | "00000000" | "00000000" | "00000000" | "11111111" | "00000000" |
| "00000111" | "00000010" | "00000010" | "00000111" | "11111000" | "00000101" |
| "00001010" | "00100010" | "00000010" | "00101010" | "11110101" | "00101000" |
Using functions
Some functions take several expressions and combine them into one: into a list, a struct, a string, or a row-wise aggregate.
let scientists = newDataFrame:
first_name: ["George", "Grace", "John", "Kurt", "Ada"]
last_name: ["Boole", "Hopper", "Tukey", "GΓΆdel", "Lovelace"]
country: ["England", "United States", "United States", "Austria-Hungary", "England"]
nbShow scientists
| first_name | last_name | country |
|---|---|---|
| str | str | str |
| "George" | "Boole" | "England" |
| "Grace" | "Hopper" | "United States" |
| "John" | "Tukey" | "United States" |
| "Kurt" | "GΓΆdel" | "Austria-Hungary" |
| "Ada" | "Lovelace" | "England" |
scientists = pl.DataFrame(
{
"first_name": ["George", "Grace", "John", "Kurt", "Ada"],
"last_name": ["Boole", "Hopper", "Tukey", "GΓΆdel", "Lovelace"],
"country": [
"England",
"United States",
"United States",
"Austria-Hungary",
"England",
],
}
)
scientists| first_name | last_name | country |
|---|---|---|
| str | str | str |
| "George" | "Boole" | "England" |
| "Grace" | "Hopper" | "United States" |
| "John" | "Tukey" | "United States" |
| "Kurt" | "GΓΆdel" | "Austria-Hungary" |
| "Ada" | "Lovelace" | "England" |
nbShow scientists.select(
concatList(col("^*_name$")).alias("concat_list"),
asStruct(all()).alias("struct"))
| concat_list | struct |
|---|---|
| list[str] | struct[3] |
| ["George", "Boole"] | {"George","Boole","England"} |
| ["Grace", "Hopper"] | {"Grace","Hopper","United States"} |
| ["John", "Tukey"] | {"John","Tukey","United States"} |
| ["Kurt", "GΓΆdel"] | {"Kurt","GΓΆdel","Austria-Hungary"} |
| ["Ada", "Lovelace"] | {"Ada","Lovelace","England"} |
scientists.select(
concat_list=pl.concat_list(pl.col("^*_name$")),
struct=pl.struct(pl.all()),
)| concat_list | struct |
|---|---|
| list[str] | struct[3] |
| ["George", "Boole"] | {"George","Boole","England"} |
| ["Grace", "Hopper"] | {"Grace","Hopper","United States"} |
| ["John", "Tukey"] | {"John","Tukey","United States"} |
| ["Kurt", "GΓΆdel"] | {"Kurt","GΓΆdel","Austria-Hungary"} |
| ["Ada", "Lovelace"] | {"Ada","Lovelace","England"} |
nbShow scientists.select(
concatStr([all()], separator = " ").alias("concat_str"),
format("{}, {} from {}", col.last_name, col.first_name, col.country).alias("format"))
| concat_str | format |
|---|---|
| str | str |
| "George Boole England" | "Boole, George from England" |
| "Grace Hopper United States" | "Hopper, Grace from United Stat⦠|
| "John Tukey United States" | "Tukey, John from United States" |
| "Kurt Gâdel Austria-Hungary" | "Gâdel, Kurt from Austria-Hunga⦠|
| "Ada Lovelace England" | "Lovelace, Ada from England" |
scientists.select(
concat_str=pl.concat_str(pl.all(), separator=" "),
format=pl.format("{}, {} from {}", "last_name", "first_name", "country"),
)| concat_str | format |
|---|---|
| str | str |
| "George Boole England" | "Boole, George from England" |
| "Grace Hopper United States" | "Hopper, Grace from United Stat⦠|
| "John Tukey United States" | "Tukey, John from United States" |
| "Kurt Gâdel Austria-Hungary" | "Gâdel, Kurt from Austria-Hunga⦠|
| "Ada Lovelace England" | "Lovelace, Ada from England" |
let prefs = newDataFrame(
series("id", [1, 7, 42, 101, 999]),
series("has_pet", [true, false, true, false, true]),
series("likes_travel", [false, false, false, false, true]),
series("likes_movies", [true, false, true, false, true]),
series("likes_books", [false, false, true, true, true]),
).withColumns(
allHorizontal(all().exclude("id")).alias("all"),
anyHorizontal(all().exclude("id")).alias("any"))
nbShow prefs
| id | has_pet | likes_travel | likes_movies | likes_books | all | any |
|---|---|---|---|---|---|---|
| i64 | bool | bool | bool | bool | bool | bool |
| 1 | true | false | true | false | false | true |
| 7 | false | false | false | false | false | false |
| 42 | true | false | true | true | false | true |
| 101 | false | false | false | true | false | true |
| 999 | true | true | true | true | true | true |
prefs = pl.DataFrame(
{
"id": [1, 7, 42, 101, 999],
"has_pet": [True, False, True, False, True],
"likes_travel": [False, False, False, False, True],
"likes_movies": [True, False, True, False, True],
"likes_books": [False, False, True, True, True],
}
).with_columns(
all=pl.all_horizontal(pl.exclude("id")),
any=pl.any_horizontal(pl.exclude("id")),
)
prefs| id | has_pet | likes_travel | likes_movies | likes_books | all | any |
|---|---|---|---|---|---|---|
| i64 | bool | bool | bool | bool | bool | bool |
| 1 | true | false | true | false | false | true |
| 7 | false | false | false | false | false | false |
| 42 | true | false | true | true | false | true |
| 101 | false | false | false | true | false | true |
| 999 | true | true | true | true | true | true |
nbShow prefs.select(
sumHorizontal(all()).alias("sum"),
maxHorizontal(all()).alias("max"),
minHorizontal(all()).alias("min"))
| sum | max | min |
|---|---|---|
| i64 | i64 | i64 |
| 4 | 1 | 0 |
| 7 | 7 | 0 |
| 46 | 42 | 0 |
| 103 | 101 | 0 |
| 1005 | 999 | 1 |
prefs.select(
sum=pl.sum_horizontal(pl.all()),
max=pl.max_horizontal(pl.all()),
min=pl.min_horizontal(pl.all()),
)| sum | max | min |
|---|---|---|
| i64 | i64 | i64 |
| 4 | 1 | 0 |
| 7 | 7 | 0 |
| 46 | 42 | 0 |
| 103 | 101 | 0 |
| 1005 | 999 | 1 |
When, then, otherwise
A conditional expression picks a value per row from the first condition that holds:
nbShow prefs.select(
col.id,
choose(
if allHorizontal(col("^likes_.*$")): "Likes everything"
elif anyHorizontal(col("^likes_.*$")): "Likes something"
else: "Likes nothing").alias("likes_what"))
| id | likes_what |
|---|---|
| i64 | str |
| 1 | "Likes something" |
| 7 | "Likes nothing" |
| 42 | "Likes something" |
| 101 | "Likes something" |
| 999 | "Likes everything" |
prefs.select(
pl.col("id"),
likes_what=pl.when(pl.all_horizontal(pl.col("^likes_.*$")))
.then(pl.lit("Likes everything"))
.when(pl.any_horizontal(pl.col("^likes_.*$")))
.then(pl.lit("Likes something"))
.otherwise(pl.lit("Likes nothing")),
)| id | likes_what |
|---|---|
| i64 | str |
| 1 | "Likes something" |
| 7 | "Likes nothing" |
| 42 | "Likes something" |
| 101 | "Likes something" |
| 999 | "Likes everything" |
Without otherwise, rows that match no condition become null:
let orders = newDataFrame:
order_amount: [500, 750, 1200, 800, 1100]
status: ["Approved", "Processing", "Processing", "Declined", "Processing"]
nbShow orders.withColumns(
choose(if col.order_amount > 1000: "Flagged").alias("status"))
| order_amount | status |
|---|---|
| i64 | str |
| 500 | null |
| 750 | null |
| 1200 | "Flagged" |
| 800 | null |
| 1100 | "Flagged" |
orders = pl.DataFrame(
{
"order_amount": [500, 750, 1200, 800, 1100],
"status": ["Approved", "Processing", "Processing", "Declined", "Processing"],
}
)
orders.with_columns(
status=pl.when(pl.col("order_amount") > 1000).then(pl.lit("Flagged"))
)| order_amount | status |
|---|---|
| i64 | str |
| 500 | null |
| 750 | null |
| 1200 | "Flagged" |
| 800 | null |
| 1100 | "Flagged" |
With otherwise(col("status")) they keep their old value:
nbShow orders.withColumns(
choose(if col.order_amount > 1000: "Flagged" else: col.status).alias("status"))
| order_amount | status |
|---|---|
| i64 | str |
| 500 | "Approved" |
| 750 | "Processing" |
| 1200 | "Flagged" |
| 800 | "Declined" |
| 1100 | "Flagged" |
orders.with_columns(
status=pl.when(pl.col("order_amount") > 1000)
.then(pl.lit("Flagged"))
.otherwise(pl.col("status"))
)| order_amount | status |
|---|---|
| i64 | str |
| 500 | "Approved" |
| 750 | "Processing" |
| 1200 | "Flagged" |
| 800 | "Declined" |
| 1100 | "Flagged" |
import nanuqbook
bookInit()
nbText: "# Combining expressions"
attribution definitiveGuide(9, "Combining Expressions",
"Data is read from `data/definitive-guide/`. Left out: " &
"`tbl_cell_numeric_alignment` (the HTML tables don't use it).")
nbText: """
Expressions get interesting when they are combined: with arithmetic, comparisons, Boolean
logic, bitwise operators, and with functions that take several expressions at once. This
chapter goes through each kind, again on the table of fruit.
"""
tabs """
import polars as pl
fruit = pl.read_csv("data/definitive-guide/fruit.csv")
fruit.filter(pl.col("is_round") & (pl.col("weight") > 1000))
""":
nbCode:
import nanuq
let fruit = readCsv("data/definitive-guide/fruit.csv")
nbShow fruit.filter(col.is_round and col.weight > 1000)
nanuq """
Python overloads `&`, `|`, `~` and `^` for expressions because its `and`, `or` and `not`
can't be overloaded. Nim's can, so Nanuq uses the words: `and`, `or`, `not`, `xor`. They
bind more loosely than comparisons, so no parentheses are needed around `col.weight > 1000`.
"""
nbText: """
## Inline operators versus methods
Each operator is also a method: `*` is `mul`. In Nim an operator already is a proc, so
`col.i * col.j` is the call `` `*`(col.i, col.j) ``; Nanuq has the method names too, for
code ported from Python.
"""
tabs """
pl.DataFrame({"i": [6.0, 0, 2, 2.5], "j": [7.0, 1, 2, 3]}).with_columns(
(pl.col("i") * pl.col("j")).alias("*"),
pl.col("i").mul(pl.col("j")).alias("Expr.mul()"),
)
""":
nbCode:
let ij = newDataFrame:
i: [6.0, 0, 2, 2.5]
j: [7.0, 1, 2, 3]
nbShow ij.withColumns(
(col.i * col.j).alias("*"),
col.i.mul(col.j).alias("Expr.mul()"))
nbText: """
## Arithmetic operations
Dividing the weight in grams by 1000 gives kilograms:
"""
tabs """
fruit.select(pl.col("name"), (pl.col("weight") / 1000))
""":
nbCode:
nbShow fruit.select(col.name, col.weight / 1000)
nbText: """
All the arithmetic operators side by side, with two decimals in the output. Python's floor
division `//` is Nim's `div`, its power `**` is `^`, and its modulo `%` is `mod`.
"""
tabs """
pl.Config(float_precision=2)
(
pl.DataFrame({"i": [0.0, 2, 2, -2, -2], "j": [1, 2, 3, 4, -5]}).with_columns(
(pl.col("i") + pl.col("j")).alias("i + j"),
(pl.col("i") - pl.col("j")).alias("i - j"),
(pl.col("i") * pl.col("j")).alias("i * j"),
(pl.col("i") / pl.col("j")).alias("i / j"),
(pl.col("i") // pl.col("j")).alias("i // j"),
(pl.col("i") ** pl.col("j")).alias("i ** j"),
(pl.col("j") % 2).alias("j % 2"),
pl.col("i").dot(pl.col("j")).alias("i β
j"),
)
)
""":
nbCode:
setFloatPrecision(2)
let arith = newDataFrame:
i: [0.0, 2, 2, -2, -2]
j: [1, 2, 3, 4, -5]
nbShow arith.withColumns(
(col.i + col.j).alias("i + j"),
(col.i - col.j).alias("i - j"),
(col.i * col.j).alias("i * j"),
(col.i / col.j).alias("i / j"),
(col.i div col.j).alias("i // j"),
(col.i ^ col.j).alias("i ** j"),
(col.j mod 2).alias("j % 2"),
col.i.dot(col.j).alias("i β
j"))
python """
pl.Config.set_float_precision()
"""
setFloatPrecision(none(int))
nbText: """
## Comparison operations
Strings compare alphabetically. `select` without a frame evaluates expressions that need
no input columns:
"""
tabs """
pl.select(pl.lit("a") > pl.lit("b"))
""":
nbCode:
nbShow select(lit("a") > lit("b"))
tabs """
fruit.select(
pl.col("name"),
pl.col("weight"),
).filter(pl.col("weight") >= 1000)
""":
nbCode:
nbShow fruit.select(col.name, col.weight).filter(col.weight >= 1000)
nbText: """
Python chains comparisons (`3 < x < 5`), but not on expressions: those need two
comparisons joined with *and*, or `isBetween`. Nim has no chained comparisons at all, so
the expression version reads like the plain one.
"""
tabs """
x = 4
pl.select((pl.lit(3) < pl.lit(x)) & (pl.lit(x) < pl.lit(5))).item()
""":
nbCode:
let x = 4
echo select(lit(3) < lit(x) and lit(x) < lit(5)).toSeries.item(bool)
tabs """
pl.select(pl.lit(x).is_between(3, 5)).item()
""":
nbCode:
echo select(lit(x).isBetween(3, 5)).toSeries.item(bool)
nbText: """
Every comparison operator, and two ways of saying "close enough" or "in a range":
"""
tabs """
pl.DataFrame(
{"a": [-273.15, 0, 42, 100], "b": [1.4142, 2.7183, 42, 3.1415]}
).with_columns(
(pl.col("a") == pl.col("b")).alias("a == b"),
(pl.col("a") <= pl.col("b")).alias("a <= b"),
(pl.all() > 0).name.suffix(" > 0"),
((pl.col("b") - pl.lit(2).sqrt()).abs() < 1e-3).alias("b β β2"),
((1 < pl.col("b")) & (pl.col("b") < 3)).alias("1 < b < 3"),
)
""":
nbCode:
let ab = newDataFrame:
a: [-273.15, 0, 42, 100]
b: [1.4142, 2.7183, 42, 3.1415]
nbShow ab.withColumns(
(col.a == col.b).alias("a == b"),
(col.a <= col.b).alias("a <= b"),
(all() > 0).name.suffix(" > 0"),
((col.b - lit(2).sqrt).abs < 1e-3).alias("b β β2"),
(1 < col.b and col.b < 3).alias("1 < b < 3"))
nbText: """
Values of different types can be compared when Polars knows how to line them up: Booleans
with numbers, a time with a time, a datetime with a date. A string and a number cannot,
so the number is cast to a string first (and then `"5" < "3"` is false).
"""
tabs """
from datetime import date, datetime, time
pl.select(
bool_num=pl.lit(True) > 0,
time_time=pl.lit(time(23, 58)) > pl.lit(time(0, 0)),
datetime_date=pl.lit(datetime(1969, 7, 21, 2, 56)) < pl.lit(date(1976, 7, 20)),
str_num=pl.lit("5") < pl.lit(3).cast(pl.String),
datetime_time=pl.lit(datetime(1999, 1, 1)).dt.time() != pl.lit(time(0, 0)),
).transpose(include_header=True, header_name="comparison", column_names=["allowed"])
""":
nbCode:
import std/times
nbShow select(
(lit(true) > 0).alias("bool_num"),
(lit(timeOfDay(23, 58)) > lit(timeOfDay(0, 0))).alias("time_time"),
(lit(dateTime(1969, mJul, 21, 2, 56, zone = utc())) <
lit(date(1976, 7, 20))).alias("datetime_date"),
(lit("5") < lit(3).cast(String)).alias("str_num"),
(lit(dateTime(1999, mJan, 1, zone = utc())).dt.time != lit(timeOfDay(0, 0))).alias("datetime_time"),
).transpose(includeHeader = true, headerName = "comparison", columnNames = ["allowed"])
nanuq """
The book writes `pl.time(23, 58)`, `pl.datetime(β¦)` and `pl.date(β¦)`, expression
constructors; the Python tab here uses literals of Python's own `datetime` values, which
give the same frame. In Nim the values are `timeOfDay`, `dateTime` (from `std/times`) and
`date`, passed to `lit`.
"""
nbText: """
## Boolean algebra operations
With `x = 7`, *p* (3 < x) is true and *q* (x < 5) is false, so *p and q* is false:
"""
tabs """
x = 7
p = pl.lit(3) < pl.lit(x)
q = pl.lit(x) < pl.lit(5)
pl.select(p & q).item()
""":
nbCode:
block:
let x = 7
let p = lit(3) < lit(x)
let q = lit(x) < lit(5)
echo select(p and q).toSeries.item(bool)
nbText: """
The whole truth table, including *nand* (β) and *nor* (β), which are the negations of
*and* and *or*:
"""
tabs """
pl.DataFrame(
{"p": [True, True, False, False], "q": [True, False, True, False]}
).with_columns(
(pl.col("p") & pl.col("q")).alias("p & q"),
(pl.col("p") | pl.col("q")).alias("p | q"),
(~pl.col("p")).alias("~p"),
(pl.col("p") ^ pl.col("q")).alias("p ^ q"),
(~(pl.col("p") & pl.col("q"))).alias("p β q"),
((pl.col("p").or_(pl.col("q"))).not_()).alias("p β q"),
)
""":
nbCode:
let pq = newDataFrame:
p: [true, true, false, false]
q: [true, false, true, false]
nbShow pq.withColumns(
(col.p and col.q).alias("p & q"),
(col.p or col.q).alias("p | q"),
(not col.p).alias("~p"),
(col.p xor col.q).alias("p ^ q"),
(not (col.p and col.q)).alias("p β q"),
(not (col.p or col.q)).alias("p β q"))
nbText: """
## Bitwise operations
On integers the same operators work bit by bit: 10 is `00001010` and 34 is `00100010`,
so 10 *or* 34 is `00101010`, which is 42.
"""
tabs """
pl.select(pl.lit(10) | pl.lit(34)).item()
""":
nbCode:
echo select(lit(10) or lit(34)).toSeries.item(int)
tabs """
bits = pl.DataFrame(
{"x": [1, 1, 0, 0, 7, 10], "y": [1, 0, 1, 0, 2, 34]},
schema={"x": pl.UInt8, "y": pl.UInt8},
).with_columns(
(pl.col("x") & pl.col("y")).alias("x & y"),
(pl.col("x") | pl.col("y")).alias("x | y"),
(~pl.col("x")).alias("~x"),
(pl.col("x") ^ pl.col("y")).alias("x ^ y"),
)
bits
""":
nbCode:
let bits = newDataFrame(
series("x", [1'u8, 1, 0, 0, 7, 10]),
series("y", [1'u8, 0, 1, 0, 2, 34]),
).withColumns(
(col.x and col.y).alias("x & y"),
(col.x or col.y).alias("x | y"),
(not col.x).alias("~x"),
(col.x xor col.y).alias("x ^ y"))
nbShow bits
nbText: """
To see the bits, each value goes through a formatting function. In Python that is
`"{0:08b}".format`; in Nim it is a proc from `std/strutils`, called by `mapElements`:
"""
tabs """
bits.select(pl.all().map_elements("{0:08b}".format, return_dtype=pl.String))
""":
nbCode:
import std/strutils
nbShow bits.select(all().mapElements(proc (v: uint8): string = v.int.toBin(8)))
nbText: """
## Using functions
Some functions take several expressions and combine them into one: into a list, a struct,
a string, or a row-wise aggregate.
"""
tabs """
scientists = pl.DataFrame(
{
"first_name": ["George", "Grace", "John", "Kurt", "Ada"],
"last_name": ["Boole", "Hopper", "Tukey", "GΓΆdel", "Lovelace"],
"country": [
"England",
"United States",
"United States",
"Austria-Hungary",
"England",
],
}
)
scientists
""":
nbCode:
let scientists = newDataFrame:
first_name: ["George", "Grace", "John", "Kurt", "Ada"]
last_name: ["Boole", "Hopper", "Tukey", "GΓΆdel", "Lovelace"]
country: ["England", "United States", "United States", "Austria-Hungary", "England"]
nbShow scientists
tabs """
scientists.select(
concat_list=pl.concat_list(pl.col("^*_name$")),
struct=pl.struct(pl.all()),
)
""":
nbCode:
nbShow scientists.select(
concatList(col("^*_name$")).alias("concat_list"),
asStruct(all()).alias("struct"))
nanuq """
Python's keyword arguments (`concat_list=β¦`) name the new columns; in Nim that is
`.alias`. The struct constructor is `asStruct`, because `struct` is the namespace
(`.struct.field(β¦)`).
"""
tabs """
scientists.select(
concat_str=pl.concat_str(pl.all(), separator=" "),
format=pl.format("{}, {} from {}", "last_name", "first_name", "country"),
)
""":
nbCode:
nbShow scientists.select(
concatStr([all()], separator = " ").alias("concat_str"),
format("{}, {} from {}", col.last_name, col.first_name, col.country).alias("format"))
tabs """
prefs = pl.DataFrame(
{
"id": [1, 7, 42, 101, 999],
"has_pet": [True, False, True, False, True],
"likes_travel": [False, False, False, False, True],
"likes_movies": [True, False, True, False, True],
"likes_books": [False, False, True, True, True],
}
).with_columns(
all=pl.all_horizontal(pl.exclude("id")),
any=pl.any_horizontal(pl.exclude("id")),
)
prefs
""":
nbCode:
let prefs = newDataFrame(
series("id", [1, 7, 42, 101, 999]),
series("has_pet", [true, false, true, false, true]),
series("likes_travel", [false, false, false, false, true]),
series("likes_movies", [true, false, true, false, true]),
series("likes_books", [false, false, true, true, true]),
).withColumns(
allHorizontal(all().exclude("id")).alias("all"),
anyHorizontal(all().exclude("id")).alias("any"))
nbShow prefs
tabs """
prefs.select(
sum=pl.sum_horizontal(pl.all()),
max=pl.max_horizontal(pl.all()),
min=pl.min_horizontal(pl.all()),
)
""":
nbCode:
nbShow prefs.select(
sumHorizontal(all()).alias("sum"),
maxHorizontal(all()).alias("max"),
minHorizontal(all()).alias("min"))
nbText: """
### When, then, otherwise
A conditional expression picks a value per row from the first condition that holds:
"""
tabs """
prefs.select(
pl.col("id"),
likes_what=pl.when(pl.all_horizontal(pl.col("^likes_.*$")))
.then(pl.lit("Likes everything"))
.when(pl.any_horizontal(pl.col("^likes_.*$")))
.then(pl.lit("Likes something"))
.otherwise(pl.lit("Likes nothing")),
)
""":
nbCode:
nbShow prefs.select(
col.id,
choose(
if allHorizontal(col("^likes_.*$")): "Likes everything"
elif anyHorizontal(col("^likes_.*$")): "Likes something"
else: "Likes nothing").alias("likes_what"))
nanuq """
`choose` turns a Nim `if`/`elif`/`else` into Polars' `when/then/otherwise`; string
values are literals. The chained `` `when`(β¦).then(β¦).otherwise(β¦) `` works too, with
backticks because `when` is a Nim keyword.
"""
nbText: """
Without `otherwise`, rows that match no condition become null:
"""
tabs """
orders = pl.DataFrame(
{
"order_amount": [500, 750, 1200, 800, 1100],
"status": ["Approved", "Processing", "Processing", "Declined", "Processing"],
}
)
orders.with_columns(
status=pl.when(pl.col("order_amount") > 1000).then(pl.lit("Flagged"))
)
""":
nbCode:
let orders = newDataFrame:
order_amount: [500, 750, 1200, 800, 1100]
status: ["Approved", "Processing", "Processing", "Declined", "Processing"]
nbShow orders.withColumns(
choose(if col.order_amount > 1000: "Flagged").alias("status"))
nbText: """
With `otherwise(col("status"))` they keep their old value:
"""
tabs """
orders.with_columns(
status=pl.when(pl.col("order_amount") > 1000)
.then(pl.lit("Flagged"))
.otherwise(pl.col("status"))
)
""":
nbCode:
nbShow orders.withColumns(
choose(if col.order_amount > 1000: "Flagged" else: col.status).alias("status"))
bookSave()