Nanuq 0.4 is the release the roadmap called "a real Series, and shaping". It adds about two hundred functions, in ten planned OpenSpec changes: math, cumulative and rolling functions, value functions (top k, recoding, binning), horizontal functions and correlation, reshaping (pivot and friends), time windows and as-of joins, a Series you can compute with, row objects, and Arraymancer tensors.
Then three more changes came from using it. Reviewing this post renamed a parameter. Writing the 0.4 explainer in Python first, and porting the Nanuq Book chapters to 0.4, turned up calls that Python users write all the time and Nanuq could not yet mirror. Sections 9 to 11 are about those.
Most of it maps onto Polars without a question: rollingMean, cumSum, topK,
unpivot are Polars' names in camelCase. This post is about the places where Nim and
Polars disagree, and what we did there. Unlike the 0.3 post, which came before the code,
every snippet here runs, on a few made-up rows shaped like NOAA's El Niรฑo index (the
subject of the 0.4 explainer).
import nanuq
let oni = newDataFrame:
season: ["1997 MJJ", "1997 JJA", "1997 JAS", "1997 ASO", "1997 SON",
"1998 DJF", "1998 JJA", "1998 SON"]
anomaly: [1.2, 1.5, 1.8, 2.1, 2.3, 2.2, -0.2, -1.1]
1. Math functions overload std/mathโ contents
sqrt, exp, floor, sin and friends already exist in std/math for floats. Nanuq
defines them again for Expr, so both work in the same module and Nim picks by
argument type. Two Polars spellings needed a decision:
clipwith one bound. Python hasclip(lower_bound=None, upper_bound=None). In Nim, two overloads with one optional bound each would makeclip(0)ambiguous. We kept it ambiguous on purpose:clip(0)doesn't compile, and you name the bound,clip(lowerBound = 0). The two-bound form is positional.- NaN is not null. Polars keeps the float NaN apart from a missing value, and so does
Nanuq:
isNan,fillNan,dropNansnext toisNull,fillNull,dropNulls.
echo oni.select(col.anomaly.clip(-1.0, 2.0).alias("clipped"),
col.anomaly.clip(lowerBound = 0.0).alias("warm_only"),
col.anomaly.abs.sqrt.round(2).alias("sqrt_abs"))
shape: (8, 3) โโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโ โ clipped โ warm_only โ sqrt_abs โ โ --- โ --- โ --- โ โ f64 โ f64 โ f64 โ โโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโโโโก โ 1.2 โ 1.2 โ 1.1 โ โ 1.5 โ 1.5 โ 1.22 โ โ 1.8 โ 1.8 โ 1.34 โ โ 2.0 โ 2.1 โ 1.45 โ โ 2.0 โ 2.3 โ 1.52 โ โ 2.0 โ 2.2 โ 1.48 โ โ -0.2 โ 0.0 โ 0.45 โ โ -1.0 โ 0.0 โ 1.05 โ โโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโ
2. Option values stay enumsโ contents
The 0.2 rule holds: a closed set of options is a pure enum named after Rust Polars. So
diff(nullBehavior = NullBehavior.Drop), interpolate(how = InterpolationMethod.Nearest),
searchSorted(x, SearchSortedSide.Left), pivot(aggregateFunction = PivotAgg.Sum),
joinAsof(strategy = AsofStrategy.Nearest) and groupByDynamic(label = Label.Right, startBy = StartBy.Monday).
One new tool turned out to be the star of the release: rleId, which numbers runs of
equal consecutive values. An El Niรฑo event is a run of at least five overlapping seasons
at +0.5 ยฐC or more, so counting events is a group-by on the run id:
let runs = oni
.withColumns((col.anomaly >= 0.5).alias("warm"))
.withColumns(col.warm.rleId.alias("run"))
.filter(col.warm)
.groupBy(col.run)
.agg(col.season.first.alias("from"), col.season.last.alias("to"),
col.anomaly.max.alias("peak"), col.season.len.alias("seasons"))
.filter(col.seasons >= 5)
echo runs
shape: (1, 5) โโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโฌโโโโโโโโโโ โ run โ from โ to โ peak โ seasons โ โ --- โ --- โ --- โ --- โ --- โ โ u32 โ str โ str โ f64 โ u32 โ โโโโโโโชโโโโโโโโโโโชโโโโโโโโโโโชโโโโโโโชโโโโโโโโโโก โ 0 โ 1997 MJJ โ 1998 DJF โ 2.3 โ 6 โ โโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโดโโโโโโโโโโ
3. Windows: rows or timeโ contents
Polars has two families of rolling functions: rolling_mean(window_size=3) counts rows,
and rolling_mean_by("date", window_size="3mo") spans time. Nanuq keeps both, and the
time window takes a std/times interval as well as Polars' duration string, as 0.3's
dateRange does: rollingMeanBy("day", 7.days) or rollingMeanBy("day", "7d").
The exponentially weighted functions take their decay as exactly one of com, span,
halfLife or alpha. Python uses None for "not given"; Nanuq uses NaN, so the
parameters stay plain floats and must be named: ewmMean(span = 3). Rust takes only
alpha, so Nanuq converts the others the way Python does.
echo oni.select(col.season, col.anomaly.rollingMean(3).round(2).alias("3-season mean"),
col.anomaly.ewmMean(span = 3).round(2).alias("ewm"))
shape: (8, 3) โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโ โ season โ 3-season mean โ ewm โ โ --- โ --- โ --- โ โ str โ f64 โ f64 โ โโโโโโโโโโโโชโโโโโโโโโโโโโโโโชโโโโโโโโก โ 1997 MJJ โ null โ 1.2 โ โ 1997 JJA โ null โ 1.4 โ โ 1997 JAS โ 1.5 โ 1.63 โ โ 1997 ASO โ 1.8 โ 1.88 โ โ 1997 SON โ 2.07 โ 2.1 โ โ 1998 DJF โ 2.2 โ 2.15 โ โ 1998 JJA โ 1.43 โ 0.97 โ โ 1998 SON โ 0.3 โ -0.07 โ โโโโโโโโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโ
4. Recoding with Nim's mapping literalโ contents
Python's replace takes a dict. Nim has no dict literal, but it has the next best thing:
{"a": "x", "b": "y"} is an array of pairs, and that is what replace and
replaceStrict take. cut and qcut bin numbers into labels; like Python Polars 2,
the result is an Enum, so the bins sort in order.
echo oni.select(col.season,
col.anomaly.cut([-0.5, 0.5, 1.5], labels = ["La Niรฑa", "neutral",
"El Niรฑo", "strong El Niรฑo"])
.alias("phase"))
shape: (8, 2) โโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ โ season โ phase โ โ --- โ --- โ โ str โ enum โ โโโโโโโโโโโโชโโโโโโโโโโโโโโโโโก โ 1997 MJJ โ El Niรฑo โ โ 1997 JJA โ El Niรฑo โ โ 1997 JAS โ strong El Niรฑo โ โ 1997 ASO โ strong El Niรฑo โ โ 1997 SON โ strong El Niรฑo โ โ 1998 DJF โ strong El Niรฑo โ โ 1998 JJA โ neutral โ โ 1998 SON โ La Niรฑa โ โโโโโโโโโโโโดโโโโโโโโโโโโโโโโโ
5. Optional parameters of several typesโ contents
This was the most interesting Nim problem of the release. Python's pivot takes
index="year" or index=["year", "month"], and join_asof takes a tolerance that is a
duration string or a number. The natural Nim spelling is a union type,
index: string | openArray[string] = [], but Nim rejects it: a default value fixes the
generic type, so a call that passes the other type no longer matches.
Overloads for every combination would multiply quickly (pivot has three such parameters). So 0.4 adds four small parameter types, each with converters from the types it accepts:
| Type | Accepts | Used by |
|---|---|---|
ColumnNames |
a name, a seq or an array of names | pivot, unpivot, partitionBy, toDummies, joinAsof's by |
OptInterval |
a std/times interval or a duration string | groupByDynamic's period and offset, rolling's offset |
JoinKey |
a column name or an expression | joinAsof's on, leftOn, rightOn |
AsofTolerance |
an interval, a duration string, an int or a float | joinAsof's tolerance |
The converters only apply where these types appear, so they don't leak into the rest of the API, and the calls read like Python:
let wide = oni
.withColumns(col.season.str.slice(0, 4).alias("year"),
col.season.str.slice(5).alias("months"))
.pivot("months", index = "year", values = "anomaly")
echo wide
echo wide.unpivot(index = "year", variableName = "months", valueName = "anomaly")
.dropNulls.sort(["year", "months"])
shape: (2, 7) โโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโฌโโโโโโโ โ year โ MJJ โ JJA โ JAS โ ASO โ SON โ DJF โ โ --- โ --- โ --- โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ f64 โ f64 โ f64 โ โโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโชโโโโโโโก โ 1997 โ 1.2 โ 1.5 โ 1.8 โ 2.1 โ 2.3 โ null โ โ 1998 โ null โ -0.2 โ null โ null โ -1.1 โ 2.2 โ โโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโดโโโโโโโ shape: (8, 3) โโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโ โ year โ months โ anomaly โ โ --- โ --- โ --- โ โ str โ str โ f64 โ โโโโโโโโชโโโโโโโโโชโโโโโโโโโโก โ 1997 โ ASO โ 2.1 โ โ 1997 โ JAS โ 1.8 โ โ 1997 โ JJA โ 1.5 โ โ 1997 โ MJJ โ 1.2 โ โ 1997 โ SON โ 2.3 โ โ 1998 โ DJF โ 2.2 โ โ 1998 โ JJA โ -0.2 โ โ 1998 โ SON โ -1.1 โ โโโโโโโโดโโโโโโโโโดโโโโโโโโโโ
6. A Series that is a thin layer over expressionsโ contents
A Series is a single named column with its values: oni["anomaly"] takes one out of a
frame. Until 0.3 Nanuq had Series only as a way to build frames. In 0.4 you can compute
with them, and the question was how to give them hundreds of methods without writing
hundreds of procs.
Every Series method is an expression in disguise. Python Polars already works this way: most of its Series methods put the Series in a one-column frame, run the matching expression on it, and take the column back out. Nanuq does the same, generated by a macro from a list of expression method names. So these two lines are the same computation:
s.rollingMean(3)
s.toFrame.select(col(s.name).rollingMean(3)).toSeries # what the macro writes
The advantage is that anything an expression can do, a Series can do too (rolling,
ranking, binning, math), and adding a method to Expr adds it to Series by adding one
name to the list. Methods that need a second column, like filter(mask) or s1 + s2,
are written by hand: they put both Series in one frame first.
Two choices differ from Python:
- Aggregations return a Series of one value. In Python,
s.max()returns a plain float or int, decided at run time by the column's type. A Nim proc must declare its return type when it is compiled, and onemaxserves float and int columns alike. Sos.maxreturns a Series holding one value, and you read it with the type you expect:s.max.item(float). If the type is wrong you get an error, not a silent conversion. - A Series is not automatically an expression. It would be handy to write
col.x + s, so we tried an implicit converter fromSeriestoExpr. But Nim's overload resolution then had two candidates fors.rollingMean(3): the Series method, and the expression method reached through the converter. It picked the expression one, which returns anExprinstead of a Series. So there is no converter. To use a Series inside an expression you wrap it,lit(s), and to add one to a frame there isdf.withColumns(s).
Here the Series is the anomaly column: its maximum and where it is, the values standardized (minus the mean, divided by the standard deviation), and the values above 2.
let s = oni["anomaly"]
echo s.max.item(float), " at row ", s.argMax.item(int)
echo ((s - s.mean.item(float)) / s.std.item(float)).round(2).toList(float)
echo s.filter(s > 2.0).toList(float)
2.3 at row 4 @[-0.02, 0.22, 0.46, 0.71, 0.87, 0.79, -1.15, -1.88] @[2.1, 2.3, 2.2]
Putting a Series back into a frame: lit
lit is short for literal: a fixed value written into the query, as opposed to col,
which looks a column up by name when the query runs. You already know lit(2.0), a
single number that Polars repeats on every row. lit(s) is the same idea for a whole
column of values: the Series goes into the query as it is, under its own name. It must
have one value per row of the frame it meets (or a single value, which is repeated).
Say we have a Series computed somewhere else, a reference anomaly for each of the eight
seasons, and we want each season's distance from it. col.anomaly - reference does not
compile, because reference is a Series and - with an expression wants another expression.
Wrapping it does:
let reference = series("reference", [1.0, 1.0, 1.5, 1.5, 2.0, 2.0, 0.0, -0.5])
echo oni.select(
col.season,
col.anomaly,
lit(reference), # the Series as a column
(col.anomaly - lit(reference)).alias("vs reference"),
)
shape: (8, 4) โโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ โ season โ anomaly โ reference โ vs reference โ โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ โโโโโโโโโโโโชโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโโโโโโโโโก โ 1997 MJJ โ 1.2 โ 1.0 โ 0.2 โ โ 1997 JJA โ 1.5 โ 1.0 โ 0.5 โ โ 1997 JAS โ 1.8 โ 1.5 โ 0.3 โ โ 1997 ASO โ 2.1 โ 1.5 โ 0.6 โ โ 1997 SON โ 2.3 โ 2.0 โ 0.3 โ โ 1998 DJF โ 2.2 โ 2.0 โ 0.2 โ โ 1998 JJA โ -0.2 โ 0.0 โ -0.2 โ โ 1998 SON โ -1.1 โ -0.5 โ -0.6 โ โโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโโโโ
To add a Series as a new column there is no need for an expression at all:
withColumns takes Series directly, as Python's with_columns does.
echo oni.withColumns(reference, s.rank.alias("rank")).head(3)
shape: (3, 4) โโโโโโโโโโโโฌโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโ โ season โ anomaly โ reference โ rank โ โ --- โ --- โ --- โ --- โ โ str โ f64 โ f64 โ f64 โ โโโโโโโโโโโโชโโโโโโโโโโชโโโโโโโโโโโโชโโโโโโโก โ 1997 MJJ โ 1.2 โ 1.0 โ 3.0 โ โ 1997 JJA โ 1.5 โ 1.0 โ 4.0 โ โ 1997 JAS โ 1.8 โ 1.5 โ 5.0 โ โโโโโโโโโโโโดโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโ
7. Rows as Nim objectsโ contents
Python returns rows as tuples or dicts. Nim has types, so you say which one you want:
df.rows(T) and df.iterRows(T) fill an object or named tuple whose fields name columns,
and toDataFrame goes the other way. Fields can be numbers, strings, booleans, dates,
Nim enums, or Option of these for columns with nulls. The implementation is plain
fieldPairs, one toList per field, so a row type costs nothing at run time beyond
reading the columns.
type Peak = object
season: string
anomaly: float
for p in oni.filter(col.anomaly > 2.0).iterRows(Peak):
echo p.season, ": +", p.anomaly, " ยฐC"
echo @[Peak(season: "2015 NDJ", anomaly: 2.6)].toDataFrame
1997 ASO: +2.1 ยฐC 1997 SON: +2.3 ยฐC 1998 DJF: +2.2 ยฐC shape: (1, 2) โโโโโโโโโโโโฌโโโโโโโโโโ โ season โ anomaly โ โ --- โ --- โ โ str โ f64 โ โโโโโโโโโโโโชโโโโโโโโโโก โ 2015 NDJ โ 2.6 โ โโโโโโโโโโโโดโโโโโโโโโโ
8. Arraymancer lives in its own moduleโ contents
Like the Datamancer bridge, the Arraymancer bridge is an optional module, so the core
library doesn't depend on Arraymancer: import nanuq/arraymancer adds
df.toTensor(float) (a 2-D tensor, one row per row, like to_numpy), toTensor for a
column or Series, and toSeries and toDataFrame back. Nulls become NaN in float
tensors and raise in integer ones. It copies; zero-copy waits for the Arrow C Data
Interface. The bridge doesn't re-export Arraymancer, so to print, slice or compute with
the tensor you import Arraymancer itself, as here.
Making this example run (it was first left unrun) found a bug. toDataFrame(tensor)
took a tensor column with Arraymancer's slicing syntax, t[_, j]. That syntax is a
macro, and inside a generic proc it is resolved where the proc is called. So
toDataFrame compiled in the tests, which import Arraymancer, and failed for a user who
imported only nanuq/arraymancer. The bridge now copies with plain t[i, j] indexing.
import arraymancer, nanuq/arraymancer
let t = oni.select(col.anomaly).toTensor(float) # 8 rows x 1 column
echo t.shape
echo t
echo t.toDataFrame(["anomaly"]).head(3)
[8, 1] Tensor[system.float] of shape "[8, 1]" on backend "Cpu" | 1.2| | 1.5| | 1.8| | 2.1| | 2.3| | 2.2| |-0.2| |-1.1| shape: (3, 1) โโโโโโโโโโโ โ anomaly โ โ --- โ โ f64 โ โโโโโโโโโโโก โ 1.2 โ โ 1.5 โ โ 1.8 โ โโโโโโโโโโโ
9. method is a keyword, so it is howโ contents
Python Polars picks the variant of rank, corr and interpolate with a parameter
called method: rank(method="dense"), pl.corr(a, b, method="spearman"). In Nim,
method is a reserved word (it declares a dynamically dispatched proc), so no parameter
can have that name.
The first drafts of 0.4 worked around it three different ways: rank(rankMethod = ...)
(from 0.3), corr(a, b, correlationMethod = ...) and interpolate(interpolation = ...).
Each was defensible alone, but together they were long and inconsistent, and you had to
remember which one each function used. We looked at four options:
| Option | Looks like | Verdict |
|---|---|---|
| A short common name | corr(a, b, how = Spearman) |
chosen |
| Separate functions, as Rust Polars does | spearmanRankCorr(a, b) |
long, and unfamiliar to Python users |
| Backticks | corr(a, b, `method` = Spearman) |
Python's name, but ugly whenever you write it |
kind |
corr(a, b, kind = Spearman) |
short, but vaguer |
how won because Polars already uses it for exactly this, "which variant": join(how= "left") and concat(how="vertical"). Now there is one rule, written into the API
conventions: where Python has method, Nanuq has how. Where Python uses another
name, Nanuq keeps it (quantile(interpolation = ...)). Most calls pass the enum by
position anyway, so they don't change:
let scores = newDataFrame:
year: [1982, 1997, 2015, 2023]
oni: [2.2, 2.4, 2.6, 2.0]
rain: [1400.0, 1700.0, 600.0, 450.0]
echo scores.select(col.year,
col.oni.rank(how = Dense, descending = true).alias("oni_rank"),
col.rain.rank(Min, descending = true).alias("rain_rank"))
echo scores.select(corr("oni", "rain").round(2).alias("pearson"),
corr("oni", "rain", how = Spearman).round(2).alias("spearman"))
shape: (4, 3) โโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโโ โ year โ oni_rank โ rain_rank โ โ --- โ --- โ --- โ โ i64 โ u32 โ u32 โ โโโโโโโโชโโโโโโโโโโโชโโโโโโโโโโโโก โ 1982 โ 3 โ 2 โ โ 1997 โ 2 โ 1 โ โ 2015 โ 1 โ 3 โ โ 2023 โ 4 โ 4 โ โโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโโ shape: (1, 2) โโโโโโโโโโโฌโโโโโโโโโโโ โ pearson โ spearman โ โ --- โ --- โ โ f64 โ f64 โ โโโโโโโโโโโชโโโโโโโโโโโก โ 0.16 โ 0.4 โ โโโโโโโโโโโดโโโโโโโโโโโ
10. An eager group-by returns a frameโ contents
In Python, df.group_by("a").agg(...) on a DataFrame returns a DataFrame, and on a
LazyFrame a LazyFrame. Until this release, Nanuq's eager groupBy was a shortcut for
df.lazy.groupBy(...), so agg returned a LazyFrame and every eager group-by ended in
.collect. Porting the book chapters made the difference obvious: every eager group-by
needed one more call in Nim than in Python.
Now a DataFrame's groupBy (and groupByDynamic and rolling) returns a small
GroupBy type, whose agg runs the query and returns a DataFrame. A LazyFrame keeps
LazyGroupBy, whose agg stays lazy. groupBy also takes column names now, as in
Python:
let byPhase = oni
.withColumns((col.anomaly >= 0.5).alias("warm"))
.groupBy("warm")
.agg(col.anomaly.mean.round(2).alias("mean"), col.season.len.alias("seasons"))
echo byPhase # a DataFrame: nothing to collect
shape: (2, 3) โโโโโโโโโฌโโโโโโโโฌโโโโโโโโโโ โ warm โ mean โ seasons โ โ --- โ --- โ --- โ โ bool โ f64 โ u32 โ โโโโโโโโโชโโโโโโโโชโโโโโโโโโโก โ true โ 1.85 โ 6 โ โ false โ -0.65 โ 2 โ โโโโโโโโโดโโโโโโโโดโโโโโโโโโโ
The change broke every .collect after an eager group-by, and here Nim had a surprise.
Once a DataFrame has no collect, df.groupBy(...).agg(...).collect still compiles: Nim
finds std/sugar's collect macro (which builds a seq from a loop) and applies it. The
query then prints as @[shape: ...], a seq holding one frame. No error, just wrong output.
So Nanuq defines collect for a DataFrame on purpose, as an error:
proc collect*(df: DataFrame): DataFrame {.error: "this is already a DataFrame " &
"(eager groupBy(...).agg(...) returns one since 0.4): drop the .collect".}
A leftover .collect now fails at compile time with that message, pointing at the right
line. It found every leftover in our tests and older posts at once.
11. What the explainer and the book asked forโ contents
Writing the El Niรฑo explainer in Python Polars first, and porting the book, gave us two lists of calls that 0.4 could not mirror. Most were small. Each one got the Python behaviour, with a Nim twist where Nim needed one. The events below are made up, shaped like the strong El Niรฑos since 1970.
Whole-row top k. The explainer's most used call was df.top_k(5, by="peak"), the five
strongest events as whole rows. 0.4 had topK only on expressions, which returns one
column. Frames now have topK(k, by, reverse) and bottomK, where by is a name, a list
of names, or expressions. Python doesn't promise an order for the result; Nanuq returns
the largest first, which is what Rust Polars does and what you want to print.
any and all are aggregations on boolean columns, with ignoreNulls as in Python.
The name all was already taken by the all() column selector; Nim overloads by
arguments, so all() stays the selector and col.x.all is the aggregation.
date from parts. pl.date(year, month, 15) builds a Date column from integer
columns. Nanuq already had date(2026, 1, 15), which returns a Nim Date value. Now, if
any part is an expression, date returns an expression instead; with three numbers it
still gives a value. We did not add Python's datetime(...) constructor: Nim names are
style-insensitive, so datetime would be the same name as std/times' dateTime, and
the two would clash in ordinary code.
floatPrecision. Python's pl.Config(float_precision=2) keeps tables readable. It
joins the other display options in setConfig and withConfig.
let events = newDataFrame:
year: [1972, 1982, 1997, 2015, 2023]
month: [11, 12, 11, 11, 12]
peak: [2.1, 2.2, 2.4, 2.6, 2.0]
floods: [false, true, true, false, false]
withConfig(floatPrecision = 1):
echo events.topK(3, by = "peak")
.withColumns(date(col.year, col.month, 15).alias("peak_month"),
(col.peak / 3.0).alias("third"))
echo events.select(col.floods.any.alias("any_floods"), col.floods.all.alias("all_floods"))
shape: (3, 6) โโโโโโโโฌโโโโโโโโฌโโโโโโโฌโโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโ โ year โ month โ peak โ floods โ peak_month โ third โ โ --- โ --- โ --- โ --- โ --- โ --- โ โ i64 โ i64 โ f64 โ bool โ date โ f64 โ โโโโโโโโชโโโโโโโโชโโโโโโโชโโโโโโโโโชโโโโโโโโโโโโโชโโโโโโโโก โ 2015 โ 11 โ 2.6 โ false โ 2015-11-15 โ 0.9 โ โ 1997 โ 11 โ 2.4 โ true โ 1997-11-15 โ 0.8 โ โ 1982 โ 12 โ 2.2 โ true โ 1982-12-15 โ 0.7 โ โโโโโโโโดโโโโโโโโดโโโโโโโดโโโโโโโโโดโโโโโโโโโโโโโดโโโโโโโโ shape: (1, 2) โโโโโโโโโโโโโโฌโโโโโโโโโโโโโ โ any_floods โ all_floods โ โ --- โ --- โ โ bool โ bool โ โโโโโโโโโโโโโโชโโโโโโโโโโโโโก โ true โ false โ โโโโโโโโโโโโโโดโโโโโโโโโโโโโ
nil as Python's None. Python writes fill_nan(None) to turn NaN into missing
values. Nim's closest word is nil, and a proc can take a parameter of type
typeof(nil), so lit(nil) is a null literal and fillNan(nil) works as in Python.
gatherEvery(n, offset) also works on frames now, keeping every n-th row.
let raw = newDataFrame:
month: [1, 2, 3, 4, 5, 6]
sst: [26.1, NaN, 26.8, 27.4, NaN, 28.0]
echo raw.withColumns(col.sst.fillNan(nil)).gatherEvery(2)
shape: (3, 2) โโโโโโโโโฌโโโโโโโ โ month โ sst โ โ --- โ --- โ โ i64 โ f64 โ โโโโโโโโโชโโโโโโโก โ 1 โ 26.1 โ โ 3 โ 26.8 โ โ 5 โ null โ โโโโโโโโโดโโโโโโโ
The longer items from the book's list (over with a mapping strategy, shuffle, hash,
extendConstant, partitionBy as a dict, printing dtypes as Python does) are in the
backlog.
Small thingsโ contents
repeat(lit("x"), 3): with a plain string, std/strutils'repeatwins the overload, so strings go throughlit.Expr.headandtaildefault to 10 values, as in Python; frames keep 5.pivotwithPivotAgg.Sumgives 0 for empty cells, as Python does.
What's nextโ contents
0.5 is about nested data: List, Array and Struct columns (explode, unnest,
str.split, Expr.valueCounts), JSON, and calling Nim from Polars (mapElements).
The 0.4 explainer is Counting El Niรฑos, and 0.5 is
designed in Designing Nanuq 0.5.