Nanuq dev log · 31 October 2026

Designing Nanuq 0.2

Looking back at the API choices behind 0.2: what Polars does, what Nim allows, and what we picked.

API design · Nanuq 0.2 · built with Nanuq 0.6.1, Polars 2.1.0 and Nim 2.2.4 · rendered with nimib

Nanuq 0.2 shipped today with data types and casts, joins, frame verbs, conditional and window expressions, column selectors and CSV options. The census post shows what they do. This post covers how their API was chosen. We wrote the explainer first, against the planned API, and every place where the story needed a feature became a question: what does Polars do, what does Nim allow, and which spelling reads best?

Unlike the 0.3 design post, everything here is implemented, so the code runs. It uses the penguins dataset.

let df = penguins()

Two conventions that shaped everything else↑ contents

Closed option sets are pure enums with Polars' names. Python Polars passes options as strings (how="left"), and Rust uses enums (JoinType::Left). Strings would be the smallest wrapper, but Nim can check enums at compile time. So every closed set is a {.pure.} enum with Rust's type and variant names: JoinType.Left, QuoteStyle.Necessary, FillNullStrategy.Forward. Because the parameter is typed, the bare how = Left works. Where Rust has no enum, the name comes from Python's strings in PascalCase (ConcatMethod.Diagonal). This is now the api-conventions spec.

Cast targets are Nim types. cast(int32) rather than cast(Int32). The DataType constants (Int64, String) still exist with Polars' names, for schemas and schemaOverrides. A small decision followed from it: dtype display names stay Polars' (i64, str), because Polars renders $df in Rust and rewriting the table renderer for Nim names wasn't worth it. cast is a Nim keyword, but keywords are fine after a dot, so only a prefix call would need backticks.

nbShow df.select(col("year").cast(int16), col("body_mass_g").cast(float32)).head(3)
shape: (3, 2)
yearbody_mass_g
i16f32
20073750.0
20073800.0
20073250.0

when/then/otherwise↑ contents

Polars starts a conditional with a free function called when, which is a Nim keyword. After a dot the keyword is fine, so only the first call is a problem. These are the five options we tested:

Option Example Verdict
A. Polars chain, backticked first call `when`(c).then("small").otherwise("big") Picked, as the reference API
B. Rename the first call When(c), whenever(c) A capitalised proc reads like a type, and other names are guesswork
C. then on the condition (c).then("small") The first when disappears and the chain reads unevenly
D. A macro over Nim's if choose: if c: "small" else: "big" Picked, as the spelling the docs use
E. A table of branches cases({c: "small"}, otherwise = "big") Every value must share one Nim type

A and D coexist: choose rewrites each branch into the chain from A, so the plan is the same. One deliberate difference from Python: a bare string in then or otherwise is a literal, not a column name. In Python, .then("small") refers to a column called "small", a well-known gotcha.

let size = choose:
  if col("body_mass_g") < 3500: "small"
  elif col("body_mass_g") < 4500: "medium"
  else: "large"
nbShow df.select(col("species"), col("body_mass_g"), size.alias("size")).head(4)
shape: (4, 3)
speciesbody_mass_gsize
stri64str
"Adelie"3750"medium"
"Adelie"3800"medium"
"Adelie"3250"small"
"Adelie"null"large"

Look at the fourth penguin. Its mass is null, so both conditions are null and the row falls through to else: an unweighed penguin counts as "large". That is Polars' behaviour, and it reads the same in Python. Check isNull first when nulls matter.

Column selectors↑ contents

Rust Polars has a Selector type, and Python a polars.selectors module (cs.numeric()). Nim already has typeclasses for numbers, so we used them. We extended the 0.2 idea that Nim types stand for dtypes:

A cs constant with methods (cs.numeric) would have compiled, but it would be one more namespace to learn. Bare numeric() and float() were ruled out, because a proc named float hides the type conversion and float(3) stops compiling. Set operators (|, &, -) are left out because col("a") - col("b") is already subtraction.

Writing the story exposed a gap: two aggregations over the same selector give duplicate names. So name.suffix and name.prefix joined the change:

nbShow df.groupBy(col("species"))
  .agg(col(SomeFloat).mean.name.suffix("_mean"), col(SomeFloat).max.name.suffix("_max"))
  .sort("species")
shape: (3, 5)
speciesbill_length_mm_meanbill_depth_mm_meanbill_length_mm_maxbill_depth_mm_max
strf64f64f64f64
"Adelie"38.79139118.34635846.021.5
"Chinstrap"48.83382418.42058858.020.8
"Gentoo"47.50487814.98211459.617.3

CSV options↑ contents

Rust configures CSV reading with a builder (CsvReadOptions::default().with_...). Python flattens the options into keyword arguments. We weighed four options: named arguments, an options object, a builder like Rust's, and both of the first two. Named arguments won: Nim's default parameters fit them naturally, and the call reads like Python. A template generates the shared signature of readCsv and scanCsv.

A few details:

let season = scanCsv("logs/*.csv",
  nullValues = ["lots", "many", "?"],
  schemaOverrides = {"bears": Int64, "collared": Boolean})
report.writeCsv("governor.csv", separator = ';', decimalComma = true)

fillNull strategies↑ contents

Rust's FillNullStrategy carries the limit inside two of its variants (Forward(limit)). A pure enum can't carry data, so limit became its own argument, as in Python, and Python's forwardFill and backwardFill shortcuts came along. An object variant that mirrors Rust exactly would have broken the bare Forward that the enum convention gives. The strategy overload sits next to the existing fillNull(value) without ambiguity.

nbShow df.select(
  col("sex"),
  col("sex").forwardFill.alias("forward"),
  col("body_mass_g").fillNull(Mean).alias("mass_or_mean"),
).slice(2, 3)
shape: (3, 3)
sexforwardmass_or_mean
strstri64
"female""female"3250
null"female"4201
"female""female"3450

A reproducible sample↑ contents

Polars samples on the eager DataFrame only, and it is random unless a seed is given. A fixed default seed would make rendered posts stable, but it would surprise anyone who expects randomness. So we followed Polars and also wrapped its global seed. One call to setRandomSeed at the top of a post or test makes every later random call repeatable, the way randomize(seed) works in std/random. n and fraction are separate overloads, so mixing them up fails at compile time.

setRandomSeed(42)
nbShow df.sample(3).select(col("species", "island", "year"))
shape: (3, 3)
speciesislandyear
strstri64
"Adelie""Biscoe"2009
"Gentoo""Biscoe"2009
"Chinstrap""Dream"2008

Things we only learned by writing code↑ contents

The process worked: an explainer first, written against the planned API, and then one OpenSpec change per area. It gave every decision a concrete example to argue over. For 0.3 we did the same, starting with the 0.3 design post.