Continuing expressions

The previous chapter introduced expressions; this one goes through the many methods an expression has, grouped by what they do to the shape of a column.

Types of operations

Example A: element-wise operations

An element-wise operation computes one value for each value: the result has as many rows as the input.

import std/math
import nanuq

let penguins = readCsv("data/definitive-guide/penguins.csv", nullValues = ["NA"]).select(
  col.species, col.island, col.sex, col.year,
  (col.body_mass_g / 1000).alias("mass"))
nbShow penguins.withColumns(
  col.mass.sqrt.alias("mass_sqrt"),
  col.mass.exp.alias("mass_exp"))
shape: (344, 7)
speciesislandsexyearmassmass_sqrtmass_exp
strstrstri64f64f64f64
"Adelie""Torgersen""male"20073.751.93649242.521082
"Adelie""Torgersen""female"20073.81.94935944.701184
"Adelie""Torgersen""female"20073.251.80277625.79034
"Adelie""Torgersen"null2007nullnullnull
"Adelie""Torgersen""female"20073.451.85741831.500392
…………………
"Chinstrap""Dream""male"20094.02.054.59815
"Chinstrap""Dream""female"20093.41.84390929.9641
"Chinstrap""Dream""male"20093.7751.94293643.597508
"Chinstrap""Dream""male"20094.12.02484660.340288
"Chinstrap""Dream""female"20093.7751.94293643.597508

Example B: operations that summarize to one

An aggregation reduces a column to one value:

nbShow penguins.select(col.mass.mean, col.island.first)
shape: (1, 2)
massisland
f64str
4.201754"Torgersen"

Example C: operations that summarize to one or more

Some operations return a number of values that depends on the data, such as the distinct values:

nbShow penguins.select(col.island.unique)
shape: (3, 1)
island
str
"Torgersen"
"Biscoe"
"Dream"

Example D: operations that extend

A few operations make a column longer: here each species is repeated 3,000 times, as a list per row that explode turns into rows, and extend_constant adds one more value.

nbShow penguins.select(
  col.species
    .unique(maintainOrder = true)
    .repeatBy(3000)
    .explode
    .extendConstant("Saiyan", n = 1))
shape: (9_001, 1)
species
str
"Adelie"
"Adelie"
"Adelie"
"Adelie"
"Adelie"
…
"Chinstrap"
"Chinstrap"
"Chinstrap"
"Chinstrap"
"Saiyan"

Element-wise operations

Operations that perform mathematical transformations

nbShow newDataFrame(series("x", [-2.0, 0.0, 0.5, 1.0, E, 1000.0])).withColumns(
  col.x.abs.alias("abs"),
  col.x.exp.alias("exp"),
  col.x.log(2).alias("log2"),
  col.x.log10.alias("log10"),
  col.x.log1p.alias("log1p"),
  col.x.sign.alias("sign"),
  col.x.sqrt.alias("sqrt"))
shape: (6, 8)
xabsexplog2log10log1psignsqrt
f64f64f64f64f64f64f64f64
-2.02.00.135335NaNNaNNaN-1.0NaN
0.00.01.0-inf-inf0.00.00.0
0.50.51.648721-1.0-0.301030.4054651.00.707107
1.01.02.7182820.00.00.6931471.01.0
2.7182822.71828215.1542621.4426950.4342941.3132621.01.648721
1000.01000.0inf9.9657843.06.9087551.031.622777

Operations related to trigonometry

The trigonometric functions work in radians; degrees and radians convert.

nbShow newDataFrame(
  series("x", [-PI, 0.0, 1.0, PI, 2 * PI, 90.0, 180.0, 360.0])
).withColumns(
  col.x.arccos.alias("arccos"),
  col.x.cos.alias("cos"),
  col.x.degrees.alias("degrees"),
  col.x.radians.alias("radians"),
  col.x.sin.alias("sin"))
shape: (8, 6)
xarccoscosdegreesradianssin
f64f64f64f64f64f64
-3.141593NaN-1.0-180.0-0.054831-1.2246e-16
0.01.5707961.00.00.00.0
1.00.00.54030257.295780.0174530.841471
3.141593NaN-1.0180.00.0548311.2246e-16
6.283185NaN1.0360.00.109662-2.4493e-16
90.0NaN-0.4480745156.6201561.5707960.893997
180.0NaN-0.5984610313.2403123.141593-0.801153
360.0NaN-0.28369120626.4806256.2831850.958916

Operations that round and categorize

cut puts each value in a bin between fixed breaks, qcut between quantiles of the data.

nbShow newDataFrame(
  series("x", [-6.0, -0.5, 0.0, 0.5, PI, 9.9, 9.99, 9.999])
).withColumns(
  col.x.ceil.alias("ceil"),
  col.x.clip(-1, 1).alias("clip"),
  col.x.cut([-1.0, 1.0], labels = ["bad", "neutral", "good"]).alias("cut"),
  col.x.floor.alias("floor"),
  col.x.qcut([0.5], labels = ["below median", "above median"]).alias("qcut"),
  col.x.round(2).alias("round2"),
  col.x.round(0).alias("round0"))
shape: (8, 8)
xceilclipcutfloorqcutround2round0
f64f64f64enumf64catf64f64
-6.0-6.0-1.0"bad"-6.0"below median"-6.0-6.0
-0.5-0.0-0.5"neutral"-1.0"below median"-0.5-0.0
0.00.00.0"neutral"0.0"below median"0.00.0
0.51.00.5"neutral"0.0"below median"0.50.0
3.1415934.01.0"good"3.0"above median"3.143.0
9.910.01.0"good"9.0"above median"9.910.0
9.9910.01.0"good"9.0"above median"9.9910.0
9.99910.01.0"good"9.0"above median"10.010.0

Operations for missing or infinite values

A float column can hold missing values (null) and the special values NaN and infinity, and each has its own tests and fills:

let xs = newDataFrame:
  x: [42.0, NaN, nil, Inf, -Inf]
nbShow xs.withColumns(
  col.x.fillNan(999).alias("fill_nan"),
  col.x.fillNull(0).alias("fill_null"),
  col.x.isFinite.alias("is_finite"),
  col.x.isInfinite.alias("is_infinite"),
  col.x.isNan.alias("is_nan"),
  col.x.isNull.alias("is_null"))
shape: (5, 7)
xfill_nanfill_nullis_finiteis_infiniteis_nanis_null
f64f64f64boolboolboolbool
42.042.042.0truefalsefalsefalse
NaN999.0NaNfalsefalsetruefalse
nullnull0.0nullnullnulltrue
infinfinffalsetruefalsefalse
-inf-inf-inffalsetruefalsefalse

To treat NaN and null alike, combine them:

nbShow xs.withColumns(
  col.x.fillNan(0).fillNull(0).alias("fill_both"),
  (col.x.isNan or col.x.isNull).alias("is_either"))
shape: (5, 3)
xfill_bothis_either
f64f64bool
42.042.0false
NaN0.0true
null0.0true
infinffalse
-inf-inffalse

Other operations

replace maps some values to others and leaves the rest alone:

nbShow newDataFrame(series("x", ["here", "there", "their", "they're"])).withColumns(
  col.x.repeatBy(3).alias("repeat_by"),
  col.x.replace({"here": "there", "they're": "they are"}).alias("replace"))
shape: (4, 3)
xrepeat_byreplace
strlist[str]str
"here"["here", "here", "here"]"there"
"there"["there", "there", "there"]"there"
"their"["their", "their", "their"]"their"
"they're"["they're", "they're", "they're"]"they are"

Nonreducing series-wise operations

These keep the length of the column, but each result depends on other values of the column, not only on its own.

Operations that accumulate

nbShow newDataFrame(
  series("x", [some(0.0), some(1.0), some(2.0), none(float), some(2.0), some(NaN),
               some(-1.0), some(2.0)])
).withColumns(
  col.x.cumCount.alias("cum_count"),
  col.x.cumMax.alias("cum_max"),
  col.x.cumMin.alias("cum_min"),
  col.x.cumProd(reverse = true).alias("cum_prod"),
  col.x.cumSum.alias("cum_sum"),
  col.x.diff.alias("diff"),
  col.x.pctChange.alias("pct_change"))
shape: (8, 8)
xcum_countcum_maxcum_mincum_prodcum_sumdiffpct_change
f64u32f64f64f64f64f64f64
0.010.00.0NaN0.0nullnull
1.021.00.0NaN1.01.0inf
2.032.00.0NaN3.01.01.0
null3nullnullnullnullnullnull
2.042.00.0NaN5.0nullnull
NaN52.00.0NaNNaNNaNNaN
-1.062.0-1.0-2.0NaNNaNNaN
2.072.0-1.02.0NaN3.0-3.0

Operations that fill and shift

Missing values can be filled from their neighbours, or interpolated; shift moves the values down (or up, with a negative number).

nbShow newDataFrame(
  series("x", [some(-1.0), some(0.0), some(1.0), none(float), none(float), some(3.0),
               some(4.0), some(NaN), some(6.0)])
).withColumns(
  col.x.backwardFill.alias("backward_fill"),
  col.x.forwardFill(limit = 1).alias("forward_fill"),
  col.x.interpolate(how = InterpolationMethod.Linear).alias("interp1"),
  col.x.interpolate(how = InterpolationMethod.Nearest).alias("interp2"),
  col.x.shift(1).alias("shift1"),
  col.x.shift(-2).alias("shift2"))
shape: (9, 7)
xbackward_fillforward_fillinterp1interp2shift1shift2
f64f64f64f64f64f64f64
-1.0-1.0-1.0-1.0-1.0null1.0
0.00.00.00.00.0-1.0null
1.01.01.01.01.00.0null
null3.01.01.6666671.01.03.0
null3.0null2.3333333.0null4.0
3.03.03.03.03.0nullNaN
4.04.04.04.04.03.06.0
NaNNaNNaNNaNNaN4.0null
6.06.06.06.06.0NaNnull

Operations related to duplicate values

nbShow newDataFrame(series("x", ["A", "C", "D", "C"])).withColumns(
  col.x.isDuplicated.alias("is_duplicated"),
  col.x.isFirstDistinct.alias("is_first_distinct"),
  col.x.isLastDistinct.alias("is_last_distinct"),
  col.x.isUnique.alias("is_unique"))
shape: (4, 5)
xis_duplicatedis_first_distinctis_last_distinctis_unique
strboolboolboolbool
"A"falsetruetruetrue
"C"truetruefalsefalse
"D"falsetruetruetrue
"C"truefalsetruefalse

Operations that compute rolling statistics

A rolling statistic looks at a window of neighbouring values; an exponentially weighted mean (EWM) weighs all earlier values, the recent ones most. On NVIDIA's closing prices in 2023:

let stock = readCsv("data/definitive-guide/nvda_2023.csv", tryParseDates = true)
  .select(col.date, col.close)
  .withColumns(
    col.close.ewmMean(com = 7, ignoreNulls = true).alias("ewm_mean"),
    col.close.rollingMean(windowSize = 7).alias("rolling_mean"),
    col.close.rollingMin(windowSize = 7).alias("rolling_min"))
nbShow stock
shape: (124, 5)
datecloseewm_meanrolling_meanrolling_min
datef64f64f64f64
2023-01-03143.149994143.149994nullnull
2023-01-04147.490005145.464667nullnull
2023-01-05142.649994144.398755nullnull
2023-01-06148.589996145.664782nullnull
2023-01-09156.279999148.388917nullnull
……………
2023-06-26406.320007407.54911425.805716406.320007
2023-06-27418.76001408.950473424.695718406.320007
2023-06-28411.170013409.227915422.445718406.320007
2023-06-29408.220001409.101926418.180006406.320007
2023-06-30423.019989410.841684417.118574406.320007

Operations that sort

nbShow newDataFrame(
  series("x", [some(1), some(3), none(int), some(3), some(7)]),
  series("y", ["D", "I", "S", "C", "O"])
).withColumns(
  col.x.argSort.alias("arg_sort"),
  col.x.shuffle(seed = 7).alias("shuffle"),
  col.x.sort(nullsLast = true).alias("sort"),
  col.x.sortBy("y").alias("sort_by"),
  col.x.reverse.alias("reverse"),
  col.x.rank.alias("rank"))
shape: (5, 8)
xyarg_sortshufflesortsort_byreverserank
i64stru32i64i64i64i64f64
1"D"271371.0
3"I"033132.5
null"S"1333nullnull
3"C"3null7732.5
7"O"41nullnull14.0

Other operations

rle_id numbers the runs of equal values:

nbShow newDataFrame(series("x", [33, 33, 27, 33, 60, 60, 60, 33, 60])).withColumns(
  col.x.rleId.alias("rle_id"))
shape: (9, 2)
xrle_id
i64u32
330
330
271
332
603
603
603
334
605

Series-wise operations that summarize to one

An aggregation in with_columns is broadcast to every row; in a group-by it gives one value per group.

nbShow newDataFrame(series("x", [1, 3, 3, 7])).withColumns(col.x.mean.alias("mean"))
shape: (4, 2)
xmean
i64f64
13.5
33.5
33.5
73.5
nbShow newDataFrame(series("cluster", ["a", "a", "b", "b"]), series("x", [1, 3, 3, 7]))
  .groupBy("cluster")
  .agg(col.x.mean.alias("mean"))
shape: (2, 2)
clustermean
strf64
"a"2.0
"b"5.0

Operations that are quantifiers

all is true when every value is, any when at least one is:

let bools = newDataFrame:
  x: [true, false, false]
  y: [true, true, true]
  z: [false, false, false]
echo bools
nbShow bools.select(
  all().all.name.suffix("_all"),
  all().any.name.suffix("_any"))
shape: (1, 6)
x_ally_allz_allx_anyy_anyz_any
boolboolboolboolboolbool
falsetruefalsetruetruefalse
shape: (3, 3)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ x     ┆ y    ┆ z     β”‚
β”‚ ---   ┆ ---  ┆ ---   β”‚
β”‚ bool  ┆ bool ┆ bool  β”‚
β•žβ•β•β•β•β•β•β•β•ͺ══════β•ͺ═══════║
β”‚ true  ┆ true ┆ false β”‚
β”‚ false ┆ true ┆ false β”‚
β”‚ false ┆ true ┆ false β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

Operations that compute statistics

nbShow penguins.select(
  col.mass.max.alias("max"),
  col.mass.mean.alias("mean"),
  col.mass.quantile(0.95).alias("quantile"),
  col.mass.skew.alias("skew"),
  col.mass.std.alias("std"),
  col.mass.sum.alias("sum"),
  col.mass.var.alias("var"))
shape: (1, 7)
maxmeanquantileskewstdsumvar
f64f64f64f64f64f64f64
6.34.2017545.650.4682640.8019551437.00.643131

Operations that count

1,729 random integers below 10,000, one of them made missing:

import std/sequtils
let dfInts = newDataFrame(series("x", toSeq(0 ..< 10_000)))
  .sample(1_729, withReplacement = true, seed = 1729)
  .withRowIndex()
  .withColumns(`when`(col.index != 403).then(col.x).alias("x"))
nbShow dfInts.slice(400, 6)
shape: (6, 2)
indexx
u32i64
4005551
401978
4023450
403null
4048411
4053448
nbShow dfInts.select(
  col.x.approxNUnique.alias("approx_n_unique"),
  col.x.count.alias("count"),
  col.x.len.alias("len"),
  col.x.nUnique.alias("n_unique"),
  col.x.nullCount.alias("null_count"))
shape: (1, 5)
approx_n_uniquecountlenn_uniquenull_count
u32u32u32u32u32
15951728172915881

The book then times n_unique against approx_n_unique on ten million integers: the approximation is several times faster. (Timings are left out of this book's builds.)

Other operations

nbShow dfInts.select(
  col.x.argMin.alias("arg_min"),
  col.x.first.alias("first"),
  col.x.get(403).alias("get"),
  col.x.implode.alias("implode"),
  col.x.last.alias("last"),
  col.x.upperBound.alias("upper_bound"))
shape: (1, 6)
arg_minfirstgetimplodelastupper_bound
u32i64i64list[i64]i64i64
2873943null[3943, 5730, … 4825]48259223372036854775807

Series-wise operations that summarize to one or more

Operations related to unique values

nbShow newDataFrame(series("x", ["A", "C", "D", "C"])).select(
  col.x.argUnique.alias("arg_unique"),
  col.x.unique(maintainOrder = true).alias("unique"),
  col.x.uniqueCounts.alias("unique_counts"),
  col.x.valueCounts(sort = true).alias("value_counts"))
shape: (3, 4)
arg_uniqueuniqueunique_countsvalue_counts
u32stru32struct[2]
0"A"1{"C",2}
1"C"2{"A",1}
2"D"1{"D",1}

Operations that select

nbShow dfInts.select(
  col.x.bottomK(7).alias("bottom_k"),
  col.x.head(7).alias("head"),
  col.x.slice(400, 7).alias("slice"),
  col.x.gather([1, 1, 2, 3, 5, 8, 13]).alias("gather"),
  col.x.gatherEvery(247).alias("gather_every"),
  col.x.topK(7).alias("top_k"))
shape: (7, 6)
bottom_kheadslicegathergather_everytop_k
i64i64i64i64i64i64
3339435551573039439992
275730978573010729978
638903450389057629964
115807null580717119962
976248411852324829961
3485233448117931279952
397941870252693439943

Operations that drop missing values

nbShow newDataFrame(
  series("x", [none(float), some(1.0), some(2.0), some(3.0), some(NaN)])
).select(
  col.x.dropNans.alias("drop_nans"), col.x.dropNulls.alias("drop_nulls"))
shape: (4, 2)
drop_nansdrop_nulls
f64f64
null1.0
1.02.0
2.03.0
3.0NaN

Other operations

mode gives the most common values (more than one when they tie):

let numbers = newDataFrame:
  x: [33, 33, 27, 33, 60, 60, 60, 33, 60]
nbShow numbers.select((col.x >= 60).argTrue.alias("arg_true"))
shape: (4, 1)
arg_true
u32
4
5
6
8
nbShow numbers.select(col.x.mode.sort.alias("mode"))
shape: (2, 1)
mode
i64
33
60

reshape turns the column into arrays, here three of three values:

nbShow numbers.select(col.x.reshape([3, 3]).alias("reshape"))
shape: (3, 1)
reshape
array[i64, 3]
[33, 33, 27]
[33, 60, 60]
[60, 33, 60]

rle gives the runs of equal values, as structs of a length and a value:

nbShow numbers.select(col.x.rle.alias("rle"))
shape: (6, 1)
rle
struct[2]
{2,33}
{1,27}
{1,33}
{3,60}
{1,33}
{1,60}

search_sorted gives the position where a value would go in a sorted column:

nbShow numbers.select(col.x.sort.searchSorted(42).alias("rle"))
shape: (1, 1)
rle
u32
5
nbShow newDataFrame(series("x", [@["a", "b"], @["c", "d"]]))
  .select(col.x.explode.alias("explode"))
shape: (4, 1)
explode
str
"a"
"b"
"c"
"d"
import nanuqbook
bookInit()

nbText: "# Continuing expressions"

attribution definitiveGuide(8, "Continuing Expressions",
  "Data is read from `data/definitive-guide/` (the NVIDIA prices are the book's " &
  "`stock/nvda/2023.csv`). The book draws random numbers with numpy; here both sides draw " &
  "them with Polars' seeded `sample`, or use the penguins, so that they get the same ones. " &
  "Left out: `hash` and `sample` on an expression (their results depend on the Polars " &
  "version), the timing cells and the plot.")

nbText: """
The previous chapter introduced expressions; this one goes through the many methods an
expression has, grouped by what they do to the shape of a column.

## Types of operations

### Example A: element-wise operations

An element-wise operation computes one value for each value: the result has as many rows
as the input.
"""

nanuq """
Python names new columns with keyword arguments (`mass=...`); Nim with `alias`.
"""

tabs """
import math
import polars as pl

penguins = pl.read_csv("data/definitive-guide/penguins.csv", null_values="NA").select(
    "species",
    "island",
    "sex",
    "year",
    mass=pl.col("body_mass_g") / 1000,
)
penguins.with_columns(
    mass_sqrt=pl.col("mass").sqrt(),
    mass_exp=pl.col("mass").exp(),
)
""":
  nbCode:
    import std/math
    import nanuq

    let penguins = readCsv("data/definitive-guide/penguins.csv", nullValues = ["NA"]).select(
      col.species, col.island, col.sex, col.year,
      (col.body_mass_g / 1000).alias("mass"))
    nbShow penguins.withColumns(
      col.mass.sqrt.alias("mass_sqrt"),
      col.mass.exp.alias("mass_exp"))

nbText: """
### Example B: operations that summarize to one

An aggregation reduces a column to one value:
"""

tabs """
penguins.select(pl.col("mass").mean(), pl.col("island").first())
""":
  nbCode:
    nbShow penguins.select(col.mass.mean, col.island.first)

nbText: """
### Example C: operations that summarize to one or more

Some operations return a number of values that depends on the data, such as the distinct
values:
"""

nanuq """
Without `maintain_order`, `unique` returns the values in any order; the book compares the
rows of both sides as a set.
"""

tabs """
penguins.select(pl.col("island").unique())
""":
  nbCode:
    nbShow penguins.select(col.island.unique)

nbText: """
### Example D: operations that extend

A few operations make a column longer: here each species is repeated 3,000 times, as a
list per row that `explode` turns into rows, and `extend_constant` adds one more value.
"""

nanuq """
The Python side adds `maintain_order=True` to `unique`, so that the 9,001 rows come in
the same order on both sides (only the first and last are shown).
"""

tabs """
penguins.select(
    pl.col("species")
    .unique(maintain_order=True)
    .repeat_by(3000)
    .explode()
    .extend_constant("Saiyan", n=1)
)
""":
  nbCode:
    nbShow penguins.select(
      col.species
        .unique(maintainOrder = true)
        .repeatBy(3000)
        .explode
        .extendConstant("Saiyan", n = 1))

nbText: """
## Element-wise operations

### Operations that perform mathematical transformations
"""

nanuq """
`log(2)` is the logarithm in base 2; `log` with no argument is the natural logarithm, as
in Python. The `math.e` of Python is `E` from std/math.
"""

tabs """
(
    pl.DataFrame({"x": [-2.0, 0.0, 0.5, 1.0, math.e, 1000.0]}).with_columns(
        abs=pl.col("x").abs(),
        exp=pl.col("x").exp(),
        log2=pl.col("x").log(2),
        log10=pl.col("x").log10(),
        log1p=pl.col("x").log1p(),
        sign=pl.col("x").sign(),
        sqrt=pl.col("x").sqrt(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("x", [-2.0, 0.0, 0.5, 1.0, E, 1000.0])).withColumns(
      col.x.abs.alias("abs"),
      col.x.exp.alias("exp"),
      col.x.log(2).alias("log2"),
      col.x.log10.alias("log10"),
      col.x.log1p.alias("log1p"),
      col.x.sign.alias("sign"),
      col.x.sqrt.alias("sqrt"))

nbText: """
### Operations related to trigonometry

The trigonometric functions work in radians; `degrees` and `radians` convert.
"""

tabs """
(
    pl.DataFrame(
        {"x": [-math.pi, 0.0, 1.0, math.pi, 2 * math.pi, 90.0, 180.0, 360.0]}
    ).with_columns(
        arccos=pl.col("x").arccos(),
        cos=pl.col("x").cos(),
        degrees=pl.col("x").degrees(),
        radians=pl.col("x").radians(),
        sin=pl.col("x").sin(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(
      series("x", [-PI, 0.0, 1.0, PI, 2 * PI, 90.0, 180.0, 360.0])
    ).withColumns(
      col.x.arccos.alias("arccos"),
      col.x.cos.alias("cos"),
      col.x.degrees.alias("degrees"),
      col.x.radians.alias("radians"),
      col.x.sin.alias("sin"))

nbText: """
### Operations that round and categorize

`cut` puts each value in a bin between fixed breaks, `qcut` between quantiles of the data.
"""

nanuq """
`clip(-1, 1)` takes both bounds; with one, name it: `clip(lowerBound = 0)`. `round` rounds
half to even by default, like Python Polars.
"""

tabs """
(
    pl.DataFrame(
        {"x": [-6.0, -0.5, 0.0, 0.5, math.pi, 9.9, 9.99, 9.999]}
    ).with_columns(
        ceil=pl.col("x").ceil(),
        clip=pl.col("x").clip(-1, 1),
        cut=pl.col("x").cut([-1, 1], labels=["bad", "neutral", "good"]),
        floor=pl.col("x").floor(),
        qcut=pl.col("x").qcut([0.5], labels=["below median", "above median"]),
        round2=pl.col("x").round(2),
        round0=pl.col("x").round(0),
    )
)
""":
  nbCode:
    nbShow newDataFrame(
      series("x", [-6.0, -0.5, 0.0, 0.5, PI, 9.9, 9.99, 9.999])
    ).withColumns(
      col.x.ceil.alias("ceil"),
      col.x.clip(-1, 1).alias("clip"),
      col.x.cut([-1.0, 1.0], labels = ["bad", "neutral", "good"]).alias("cut"),
      col.x.floor.alias("floor"),
      col.x.qcut([0.5], labels = ["below median", "above median"]).alias("qcut"),
      col.x.round(2).alias("round2"),
      col.x.round(0).alias("round0"))

nbText: """
### Operations for missing or infinite values

A float column can hold missing values (`null`) and the special values NaN and infinity,
and each has its own tests and fills:
"""

nanuq """
In Nim a missing value is `none(float)`, and NaN and infinity are `NaN` and `Inf`.
"""

tabs """
x = [42.0, math.nan, None, math.inf, -math.inf]
(
    pl.DataFrame({"x": x}).with_columns(
        fill_nan=pl.col("x").fill_nan(999),
        fill_null=pl.col("x").fill_null(0),
        is_finite=pl.col("x").is_finite(),
        is_infinite=pl.col("x").is_infinite(),
        is_nan=pl.col("x").is_nan(),
        is_null=pl.col("x").is_null(),
    )
)
""":
  nbCode:
    let xs = newDataFrame:
      x: [42.0, NaN, nil, Inf, -Inf]
    nbShow xs.withColumns(
      col.x.fillNan(999).alias("fill_nan"),
      col.x.fillNull(0).alias("fill_null"),
      col.x.isFinite.alias("is_finite"),
      col.x.isInfinite.alias("is_infinite"),
      col.x.isNan.alias("is_nan"),
      col.x.isNull.alias("is_null"))

nbText: "To treat NaN and null alike, combine them:"

tabs """
(
    pl.DataFrame({"x": x}).with_columns(
        fill_both=pl.col("x").fill_nan(0).fill_null(0),
        is_either=(pl.col("x").is_nan() | pl.col("x").is_null()),
    )
)
""":
  nbCode:
    nbShow xs.withColumns(
      col.x.fillNan(0).fillNull(0).alias("fill_both"),
      (col.x.isNan or col.x.isNull).alias("is_either"))

nbText: """
### Other operations

`replace` maps some values to others and leaves the rest alone:
"""

nanuq """
Nim writes the mapping of `replace` as `{"a": "b"}`. The book also shows
`hash(seed=1337)`, which Nanuq has (`col.x.hash(seed = 1337)`), but its values depend on
the Polars version, and Nanuq's Rust Polars is not the one inside Python Polars 2.1, so
that column is left out.
"""

tabs """
(
    pl.DataFrame({"x": ["here", "there", "their", "they're"]}).with_columns(
        repeat_by=pl.col("x").repeat_by(3),
        replace=pl.col("x").replace(
            {
                "here": "there",
                "they're": "they are",
            }
        ),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("x", ["here", "there", "their", "they're"])).withColumns(
      col.x.repeatBy(3).alias("repeat_by"),
      col.x.replace({"here": "there", "they're": "they are"}).alias("replace"))

nbText: """
## Nonreducing series-wise operations

These keep the length of the column, but each result depends on other values of the
column, not only on its own.

### Operations that accumulate
"""

tabs """
import numpy as np

(
    pl.DataFrame(
        {"x": [0.0, 1.0, 2.0, None, 2.0, np.nan, -1.0, 2.0]}
    ).with_columns(
        cum_count=pl.col("x").cum_count(),
        cum_max=pl.col("x").cum_max(),
        cum_min=pl.col("x").cum_min(),
        cum_prod=pl.col("x").cum_prod(reverse=True),
        cum_sum=pl.col("x").cum_sum(),
        diff=pl.col("x").diff(),
        pct_change=pl.col("x").pct_change(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(
      series("x", [some(0.0), some(1.0), some(2.0), none(float), some(2.0), some(NaN),
                   some(-1.0), some(2.0)])
    ).withColumns(
      col.x.cumCount.alias("cum_count"),
      col.x.cumMax.alias("cum_max"),
      col.x.cumMin.alias("cum_min"),
      col.x.cumProd(reverse = true).alias("cum_prod"),
      col.x.cumSum.alias("cum_sum"),
      col.x.diff.alias("diff"),
      col.x.pctChange.alias("pct_change"))

nbText: """
### Operations that fill and shift

Missing values can be filled from their neighbours, or interpolated; `shift` moves the
values down (or up, with a negative number).
"""

nanuq """
Python's `method` is `how` in Nanuq (as for `rank` and `corr`), a value of the enum
`InterpolationMethod`: `Linear` or `Nearest`.
"""

tabs """
(
    pl.DataFrame(
        {"x": [-1.0, 0.0, 1.0, None, None, 3.0, 4.0, math.nan, 6.0]}
    ).with_columns(
        backward_fill=pl.col("x").backward_fill(),
        forward_fill=pl.col("x").forward_fill(limit=1),
        interp1=pl.col("x").interpolate(method="linear"),
        interp2=pl.col("x").interpolate(method="nearest"),
        shift1=pl.col("x").shift(1),
        shift2=pl.col("x").shift(-2),
    )
)
""":
  nbCode:
    nbShow newDataFrame(
      series("x", [some(-1.0), some(0.0), some(1.0), none(float), none(float), some(3.0),
                   some(4.0), some(NaN), some(6.0)])
    ).withColumns(
      col.x.backwardFill.alias("backward_fill"),
      col.x.forwardFill(limit = 1).alias("forward_fill"),
      col.x.interpolate(how = InterpolationMethod.Linear).alias("interp1"),
      col.x.interpolate(how = InterpolationMethod.Nearest).alias("interp2"),
      col.x.shift(1).alias("shift1"),
      col.x.shift(-2).alias("shift2"))

nbText: "### Operations related to duplicate values"

tabs """
(
    pl.DataFrame({"x": ["A", "C", "D", "C"]}).with_columns(
        is_duplicated=pl.col("x").is_duplicated(),
        is_first_distinct=pl.col("x").is_first_distinct(),
        is_last_distinct=pl.col("x").is_last_distinct(),
        is_unique=pl.col("x").is_unique(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("x", ["A", "C", "D", "C"])).withColumns(
      col.x.isDuplicated.alias("is_duplicated"),
      col.x.isFirstDistinct.alias("is_first_distinct"),
      col.x.isLastDistinct.alias("is_last_distinct"),
      col.x.isUnique.alias("is_unique"))

nbText: """
### Operations that compute rolling statistics

A rolling statistic looks at a window of neighbouring values; an exponentially weighted
mean (EWM) weighs all earlier values, the recent ones most. On NVIDIA's closing prices in
2023:
"""

nanuq """
`ewmMean` takes the decay by name, as one of `com`, `span`, `halfLife` or `alpha`. The
book then plots the three series with plotnine; the plot is left out.
"""

tabs """
stock = (
    pl.read_csv("data/definitive-guide/nvda_2023.csv", try_parse_dates=True)
    .select("date", "close")
    .with_columns(
        ewm_mean=pl.col("close").ewm_mean(com=7, ignore_nulls=True),
        rolling_mean=pl.col("close").rolling_mean(window_size=7),
        rolling_min=pl.col("close").rolling_min(window_size=7),
    )
)

stock
""":
  nbCode:
    let stock = readCsv("data/definitive-guide/nvda_2023.csv", tryParseDates = true)
      .select(col.date, col.close)
      .withColumns(
        col.close.ewmMean(com = 7, ignoreNulls = true).alias("ewm_mean"),
        col.close.rollingMean(windowSize = 7).alias("rolling_mean"),
        col.close.rollingMin(windowSize = 7).alias("rolling_min"))
    nbShow stock

nbText: "### Operations that sort"

tabs """
(
    pl.DataFrame(
        {
            "x": [1, 3, None, 3, 7],
            "y": ["D", "I", "S", "C", "O"],
        }
    ).with_columns(
        arg_sort=pl.col("x").arg_sort(),
        shuffle=pl.col("x").shuffle(seed=7),
        sort=pl.col("x").sort(nulls_last=True),
        sort_by=pl.col("x").sort_by("y"),
        reverse=pl.col("x").reverse(),
        rank=pl.col("x").rank(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(
      series("x", [some(1), some(3), none(int), some(3), some(7)]),
      series("y", ["D", "I", "S", "C", "O"])
    ).withColumns(
      col.x.argSort.alias("arg_sort"),
      col.x.shuffle(seed = 7).alias("shuffle"),
      col.x.sort(nullsLast = true).alias("sort"),
      col.x.sortBy("y").alias("sort_by"),
      col.x.reverse.alias("reverse"),
      col.x.rank.alias("rank"))

nbText: """
### Other operations

`rle_id` numbers the runs of equal values:
"""

tabs """
(
    pl.DataFrame({"x": [33, 33, 27, 33, 60, 60, 60, 33, 60]}).with_columns(
        rle_id=pl.col("x").rle_id(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("x", [33, 33, 27, 33, 60, 60, 60, 33, 60])).withColumns(
      col.x.rleId.alias("rle_id"))

nbText: """
## Series-wise operations that summarize to one

An aggregation in `with_columns` is broadcast to every row; in a group-by it gives one
value per group.
"""

tabs """
(
    pl.DataFrame({"x": [1, 3, 3, 7]}).with_columns(
        mean=pl.col("x").mean(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("x", [1, 3, 3, 7])).withColumns(col.x.mean.alias("mean"))

tabs """
(
    pl.DataFrame({"cluster": ["a", "a", "b", "b"], "x": [1, 3, 3, 7]})
    .group_by("cluster")
    .agg(
        mean=pl.col("x").mean(),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("cluster", ["a", "a", "b", "b"]), series("x", [1, 3, 3, 7]))
      .groupBy("cluster")
      .agg(col.x.mean.alias("mean"))

nbText: """
### Operations that are quantifiers

`all` is true when every value is, `any` when at least one is:
"""


tabs """
df = pl.DataFrame(
    {
        "x": [True, False, False],
        "y": [True, True, True],
        "z": [False, False, False],
    }
)
print(df)
df.select(
    pl.all().all().name.suffix("_all"),
    pl.all().any().name.suffix("_any"),
)
""":
  nbCode:
    let bools = newDataFrame:
      x: [true, false, false]
      y: [true, true, true]
      z: [false, false, false]
    echo bools
    nbShow bools.select(
      all().all.name.suffix("_all"),
      all().any.name.suffix("_any"))

nbText: """
### Operations that compute statistics
"""

nanuq """
The book computes these on a million numbers drawn from a normal distribution with numpy.
Nanuq can't draw the same numbers, so both sides use the penguins' masses instead.
"""

tabs """
(
    penguins.select(
        max=pl.col("mass").max(),
        mean=pl.col("mass").mean(),
        quantile=pl.col("mass").quantile(quantile=0.95),
        skew=pl.col("mass").skew(),
        std=pl.col("mass").std(),
        sum=pl.col("mass").sum(),
        var=pl.col("mass").var(),
    )
)
""":
  nbCode:
    nbShow penguins.select(
      col.mass.max.alias("max"),
      col.mass.mean.alias("mean"),
      col.mass.quantile(0.95).alias("quantile"),
      col.mass.skew.alias("skew"),
      col.mass.std.alias("std"),
      col.mass.sum.alias("sum"),
      col.mass.var.alias("var"))

nbText: """
### Operations that count

1,729 random integers below 10,000, one of them made missing:
"""

nanuq """
The book draws the integers with numpy and sets value 403 to `None` in place. Here both
sides draw them with Polars' `sample` and a seed, and blank value 403 with a `when` that
has no `otherwise`.
"""

tabs """
df_ints = (
    pl.DataFrame({"x": range(10_000)})
    .sample(1_729, with_replacement=True, seed=1729)
    .with_row_index()
    .with_columns(x=pl.when(pl.col("index") != 403).then(pl.col("x")))
)
df_ints.slice(400, 6)
""":
  nbCode:
    import std/sequtils
    let dfInts = newDataFrame(series("x", toSeq(0 ..< 10_000)))
      .sample(1_729, withReplacement = true, seed = 1729)
      .withRowIndex()
      .withColumns(`when`(col.index != 403).then(col.x).alias("x"))
    nbShow dfInts.slice(400, 6)

tabs """
df_ints.select(
    approx_n_unique=pl.col("x").approx_n_unique(),
    count=pl.col("x").count(),
    len=pl.col("x").len(),
    n_unique=pl.col("x").n_unique(),
    null_count=pl.col("x").null_count(),
)
""":
  nbCode:
    nbShow dfInts.select(
      col.x.approxNUnique.alias("approx_n_unique"),
      col.x.count.alias("count"),
      col.x.len.alias("len"),
      col.x.nUnique.alias("n_unique"),
      col.x.nullCount.alias("null_count"))

nbText: """
The book then times `n_unique` against `approx_n_unique` on ten million integers: the
approximation is several times faster. (Timings are left out of this book's builds.)

### Other operations
"""

tabs """
df_ints.select(
    arg_min=pl.col("x").arg_min(),
    first=pl.col("x").first(),
    get=pl.col("x").get(403),
    implode=pl.col("x").implode(),
    last=pl.col("x").last(),
    upper_bound=pl.col("x").upper_bound(),
)
""":
  nbCode:
    nbShow dfInts.select(
      col.x.argMin.alias("arg_min"),
      col.x.first.alias("first"),
      col.x.get(403).alias("get"),
      col.x.implode.alias("implode"),
      col.x.last.alias("last"),
      col.x.upperBound.alias("upper_bound"))

nbText: """
## Series-wise operations that summarize to one or more

### Operations related to unique values
"""

tabs """
(
    pl.DataFrame({"x": ["A", "C", "D", "C"]}).select(
        arg_unique=pl.col("x").arg_unique(),
        unique=pl.col("x").unique(maintain_order=True),
        unique_counts=pl.col("x").unique_counts(),
        value_counts=pl.col("x").value_counts(sort=True),
    )
)
""":
  nbCode:
    nbShow newDataFrame(series("x", ["A", "C", "D", "C"])).select(
      col.x.argUnique.alias("arg_unique"),
      col.x.unique(maintainOrder = true).alias("unique"),
      col.x.uniqueCounts.alias("unique_counts"),
      col.x.valueCounts(sort = true).alias("value_counts"))

nbText: "### Operations that select"

nanuq """
The book also takes a random `sample(7)`. Nanuq has it (`col.x.sample(7, seed = 42)`),
but even with a seed the order of the values depends on the Polars version, which differs
between Nanuq and Python Polars 2.1, so that column is left out.
"""

tabs """
df_ints.select(
    bottom_k=pl.col("x").bottom_k(7),
    head=pl.col("x").head(7),
    slice=pl.col("x").slice(400, 7),
    gather=pl.col("x").gather([1, 1, 2, 3, 5, 8, 13]),
    gather_every=pl.col("x").gather_every(247),
    top_k=pl.col("x").top_k(7),
)
""":
  nbCode:
    nbShow dfInts.select(
      col.x.bottomK(7).alias("bottom_k"),
      col.x.head(7).alias("head"),
      col.x.slice(400, 7).alias("slice"),
      col.x.gather([1, 1, 2, 3, 5, 8, 13]).alias("gather"),
      col.x.gatherEvery(247).alias("gather_every"),
      col.x.topK(7).alias("top_k"))

nbText: "### Operations that drop missing values"

tabs """
x = [None, 1.0, 2.0, 3.0, np.nan]
(
    pl.DataFrame({"x": x}).select(
        drop_nans=pl.col("x").drop_nans(), drop_nulls=pl.col("x").drop_nulls()
    )
)
""":
  nbCode:
    nbShow newDataFrame(
      series("x", [none(float), some(1.0), some(2.0), some(3.0), some(NaN)])
    ).select(
      col.x.dropNans.alias("drop_nans"), col.x.dropNulls.alias("drop_nulls"))

nbText: """
### Other operations

`mode` gives the most common values (more than one when they tie):
"""

tabs """
numbers = [33, 33, 27, 33, 60, 60, 60, 33, 60]

(
    pl.DataFrame({"x": numbers}).select(
        arg_true=(pl.col("x") >= 60).arg_true(),
    )
)
""":
  nbCode:
    let numbers = newDataFrame:
      x: [33, 33, 27, 33, 60, 60, 60, 33, 60]
    nbShow numbers.select((col.x >= 60).argTrue.alias("arg_true"))

tabs """
(
    pl.DataFrame({"x": numbers}).select(
        mode=pl.col("x").mode().sort(),
    )
)
""":
  nbCode:
    nbShow numbers.select(col.x.mode.sort.alias("mode"))

nbText: "`reshape` turns the column into arrays, here three of three values:"

tabs """
(
    pl.DataFrame({"x": numbers}).select(
        reshape=pl.col("x").reshape((3, 3)),
    )
)
""":
  nbCode:
    nbShow numbers.select(col.x.reshape([3, 3]).alias("reshape"))

nbText: "`rle` gives the runs of equal values, as structs of a length and a value:"

tabs """
(
    pl.DataFrame({"x": numbers}).select(
        rle=pl.col("x").rle(),
    )
)
""":
  nbCode:
    nbShow numbers.select(col.x.rle.alias("rle"))

nbText: """
`search_sorted` gives the position where a value would go in a sorted column:
"""

tabs """
(
    pl.DataFrame({"x": numbers}).select(
        rle=pl.col("x").sort().search_sorted(42),
    )
)
""":
  nbCode:
    nbShow numbers.select(col.x.sort.searchSorted(42).alias("rle"))

nanuq """
The chapter ends with series-wise operations that extend: `explode` turns each list into
rows.
"""

tabs """
(
    pl.DataFrame(
        {
            "x": [["a", "b"], ["c", "d"]],
        }
    ).select(explode=pl.col("x").explode())
)
""":
  nbCode:
    nbShow newDataFrame(series("x", [@["a", "b"], @["c", "d"]]))
      .select(col.x.explode.alias("explode"))

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