Textual and temporal data types

Beyond numbers, a column can hold text, a fixed set of categories, dates and times, or nested values. Each of these types has its own namespace of expressions: str, cat, dt, list, arr and struct. Nanuq has all six.

String

String Examples

Three messy lines of text:

import nanuq

var corpus = newDataFrame:
  raw_text: ["  Data Science is amazing ", "Data_analysis > Data entry",
             " Python&Polars; Fast"]

nbShow corpus
shape: (3, 1)
raw_text
str
"  Data Science is amazing "
"Data_analysis > Data entry"
" Python&Polars; Fast"

String expressions chain, each step working on the result of the one before:

corpus = corpus.withColumns(
  col.raw_text
    .str.stripChars
    .str.toLowercase
    .str.replaceAll("_", " ")
    .alias("processed_text"))
nbShow corpus
shape: (3, 2)
raw_textprocessed_text
strstr
"  Data Science is amazing ""data science is amazing"
"Data_analysis > Data entry""data analysis > data entry"
" Python&Polars; Fast""python&polars; fast"
nbShow corpus.withColumns(
  col.processed_text.str.slice(0, 5).alias("first_5_chars"),
  col.processed_text
    .str.split(" ")
    .list.get(0)
    .alias("first_word"),
  col.processed_text.str.split(" ").list.get(1).alias("second_word"))
shape: (3, 5)
raw_textprocessed_textfirst_5_charsfirst_wordsecond_word
strstrstrstrstr
"  Data Science is amazing ""data science is amazing""data ""data""science"
"Data_analysis > Data entry""data analysis > data entry""data ""data""analysis"
" Python&Polars; Fast""python&polars; fast""pytho""python&polars;""fast"
nbShow corpus.withColumns(
  col.processed_text.str.lenChars.alias("len_chars"),
  col.processed_text.str.lenBytes.alias("len_bytes"),
  col.processed_text.str.countMatches("a").alias("count_a"))
shape: (3, 5)
raw_textprocessed_textlen_charslen_bytescount_a
strstru32u32u32
"  Data Science is amazing ""data science is amazing"23234
"Data_analysis > Data entry""data analysis > data entry"26266
" Python&Polars; Fast""python&polars; fast"19192

Extracting all the hashtags of a post gives a list per row:

let posts = newDataFrame:
  post: ["Loving #python and #polars!", "A boomer post without a hashtag"]

let hashtagRegex = r"#(\w+)"

nbShow posts.withColumns(col.post.str.extractAll(hashtagRegex).alias("hashtags"))
shape: (2, 2)
posthashtags
strlist[str]
"Loving #python and #polars!"["#python", "#polars"]
"A boomer post without a hashta…[]

Categorical

A categorical column stores each distinct string once, and a small integer per row: its physical representation.

let cats = newDataFrame(
    series("name", ["Persian cat", "Siamese Cat", "Lynx", "Lynx"]))
  .cast({"name": Categorical})

nbShow cats.withColumns(col.name.toPhysical.alias("name_physical"))
shape: (4, 2)
namename_physical
catu32
"Persian cat"0
"Siamese Cat"1
"Lynx"2
"Lynx"2

Categorical Examples

A second frame with some of the same cats. In recent versions of Polars all categoricals share one global set of categories, so the same name has the same code in both frames:

let moreCats = newDataFrame(
    series("name", ["Maine Coon Cat", "Lynx", "Lynx", "Siamese Cat"]))
  .cast({"name": Categorical})

nbShow moreCats.withColumns(col.name.toPhysical.alias("name_physical"))
shape: (4, 2)
namename_physical
catu32
"Maine Coon Cat"3
"Lynx"2
"Lynx"2
"Siamese Cat"1
nbShow cats.join(moreCats, on = ["name"])
shape: (5, 1)
name
cat
"Lynx"
"Lynx"
"Lynx"
"Lynx"
"Siamese Cat"

Sorting a categorical column sorts by the strings, not by the codes:

let sortingComparisonDf = cats.select(col.name.alias("cat_lexical")).withColumns(
  col.cat_lexical.toPhysical.alias("cat_physical"))

nbShow sortingComparisonDf.sort("cat_lexical")
shape: (4, 2)
cat_lexicalcat_physical
catu32
"Lynx"2
"Lynx"2
"Persian cat"0
"Siamese Cat"1

Enum

An Enum has a fixed list of categories, given up front, in order:

type
  Bear = enum
    Polar, Panda, Brown

let bearEnumSeries = series("enum", [Polar, Panda, Brown, Brown, Polar])

let bearCatSeries = series("cat", ["Polar", "Panda", "Brown", "Brown", "Polar"])

nbShow newDataFrame(bearEnumSeries, bearCatSeries).cast({"cat": Categorical})
shape: (5, 2)
enumcat
enumcat
"Polar""Polar"
"Panda""Panda"
"Brown""Brown"
"Brown""Brown"
"Polar""Polar"

Temporal

Temporal Examples

Loading from a CSV file

With try_parse_dates, the CSV reader recognises the dates:

nbShow readCsv("data/definitive-guide/all_stocks.csv", tryParseDates = true)
shape: (18_476, 8)
symboldateopenhighlowcloseadj closevolume
strdatef64f64f64f64f64i64
"ASML"1999-01-0411.76562512.2812511.76562512.1406257.5225231801867
"ASML"1999-01-0511.85937514.2511.7187513.968758.6552578241600
"ASML"1999-01-0614.2517.60156314.20312516.87510.45601816400267
"ASML"1999-01-0714.74218817.812514.5312516.85156310.44149517722133
"ASML"1999-01-0816.07812516.28906315.02343815.7968759.78799510696000
……………………
"TSM"2023-06-26102.019997103.040001100.089996100.11000199.1259548560000
"TSM"2023-06-27101.150002102.790001100.019997102.080002101.0765919732000
"TSM"2023-06-28100.5101.879997100.220001100.91999899.9279868160900
"TSM"2023-06-29101.339996101.519997100.019997100.63999999.6507427383900
"TSM"2023-06-30101.400002101.889999100.410004100.91999899.92798611701700

Converting to and from a String

let dates = newDataFrame(series("date_str", ["2023-12-31", "2024-02-29"])).withColumns(
  col.date_str.str.toDate("%Y-%m-%d").alias("date"))

nbShow dates
shape: (2, 2)
date_strdate
strdate
"2023-12-31"2023-12-31
"2024-02-29"2024-02-29
nbShow dates.withColumns(col.date.dt.toString("%d-%m-%Y").alias("formatted_date"))
shape: (2, 3)
date_strdateformatted_date
strdatestr
"2023-12-31"2023-12-31"31-12-2023"
"2024-02-29"2024-02-29"29-02-2024"

Generating date ranges

nbShow newDataFrame().select(
  dateRange(start = date(2024, 10, 28), stop = date(2024, 12, 1), interval = 1.weeks)
    .alias("monday"))
shape: (5, 1)
monday
date
2024-10-28
2024-11-04
2024-11-11
2024-11-18
2024-11-25

Time zones

Strings with mixed UTC offsets parse into one UTC column, which can then be shown in a time zone:

nbShow newDataFrame(series("utc_mixed_offset", [
    "2021-03-27T00:00:00+0100",
    "2021-03-28T00:00:00+0100",
    "2021-03-29T00:00:00+0200",
    "2021-03-30T00:00:00+0200"]))
  .withColumns(
    col.utc_mixed_offset.str.toDatetime("%Y-%m-%dT%H:%M:%S%z").alias("parsed"))
  .withColumns(
    col.parsed.dt.convertTimeZone("Europe/Amsterdam").alias("converted"))
shape: (4, 3)
utc_mixed_offsetparsedconverted
strdatetime[ΞΌs, UTC]datetime[ΞΌs, Europe/Amsterdam]
"2021-03-27T00:00:00+0100"2021-03-26 23:00:00 UTC2021-03-27 00:00:00 CET
"2021-03-28T00:00:00+0100"2021-03-27 23:00:00 UTC2021-03-28 00:00:00 CET
"2021-03-29T00:00:00+0200"2021-03-28 22:00:00 UTC2021-03-29 00:00:00 CEST
"2021-03-30T00:00:00+0200"2021-03-29 22:00:00 UTC2021-03-30 00:00:00 CEST

List

List Examples

A list column holds a list of values of the same type in each row, of any length. Some list methods reduce each list to one value:

let bools = newDataFrame:
  values: [@[true, true], @[false, false, true], @[false]]

nbShow bools.withColumns(
  col.values.list.all.alias("all_true"),
  col.values.list.any.alias("any_true"))
shape: (3, 3)
valuesall_trueany_true
list[bool]boolbool
[true, true]truetrue
[false, false, true]falsetrue
[false]falsefalse

list.eval runs any expression on the values of each list, with pl.element() standing for them:

let groups = newDataFrame:
  ages: [@[18, 21], @[30, 40, 50], @[42, 69]]

nbShow groups.withColumns(
  col.ages.list.eval(element() > 40).alias("over_forty")
).withColumns(
  col.over_forty.list.all.alias("all_over_forty"))
shape: (3, 3)
agesover_fortyall_over_forty
list[i64]list[bool]bool
[18, 21][false, false]false
[30, 40, 50][false, false, true]false
[42, 69][true, true]true
nbShow groups.withColumns(
  col.ages.list.sort(descending = true).alias("ages_sorted_descending"))
shape: (3, 2)
agesages_sorted_descending
list[i64]list[i64]
[18, 21][21, 18]
[30, 40, 50][50, 40, 30]
[42, 69][69, 42]

Exploding gives one row per value, on the frame or on one column:

nbShow groups.explode("ages")
shape: (7, 1)
ages
i64
18
21
30
40
50
42
69
nbShow groups.select(col.ages.list.explode.alias("ages"))
shape: (7, 1)
ages
i64
18
21
30
40
50
42
69

Array

Array Examples

An array column holds the same number of values in every row, like a week of temperatures:

let events = newDataFrame:
  location: ["Paris", "Amsterdam", "Barcelona"]
  temperatures: [[23, 27, 21, 22, 24, 23, 22],
                 [17, 19, 15, 22, 18, 20, 21],
                 [30, 32, 28, 29, 34, 33, 31]]

nbShow events
shape: (3, 2)
locationtemperatures
strarray[i64, 7]
"Paris"[23, 27, … 22]
"Amsterdam"[17, 19, … 21]
"Barcelona"[30, 32, … 31]
nbShow events.withColumns(
  col.temperatures.arr.median.alias("median"),
  col.temperatures.arr.max.alias("max"),
  col.temperatures.arr.argMax.alias("warmest_dow"))
shape: (3, 5)
locationtemperaturesmedianmaxwarmest_dow
strarray[i64, 7]f64i64u32
"Paris"[23, 27, … 22]23.0271
"Amsterdam"[17, 19, … 21]19.0223
"Barcelona"[30, 32, … 31]31.0344

Struct

Struct Examples

A struct column holds a few named fields in each row:

let orders = newDataFrame:
  customer_id: [2781, 6139, 5392]
  order_details: [
    (amount: 250.00, date: date(2024, 1, 3), items: 5),
    (amount: 150.00, date: date(2024, 1, 5), items: 1),
    (amount: 100.00, date: date(2024, 1, 2), items: 3)]

nbShow orders
shape: (3, 2)
customer_idorder_details
i64struct[3]
2781{250.0,2024-01-03,5}
6139{150.0,2024-01-05,1}
5392{100.0,2024-01-02,3}
nbShow orders.select(col.order_details.struct.field("amount"))
shape: (3, 1)
amount
f64
250.0
150.0
100.0
let orderDetails = orders.unnest("order_details")

nbShow orderDetails
shape: (3, 4)
customer_idamountdateitems
i64f64datei64
2781250.02024-01-035
6139150.02024-01-051
5392100.02024-01-023
nbShow orderDetails.select(
  col.amount,
  col.date,
  col.items,
  asStruct(col.amount, col.date, col.items).alias("order_details"))
shape: (3, 4)
amountdateitemsorder_details
f64datei64struct[3]
250.02024-01-035{250.0,2024-01-03,5}
150.02024-01-051{150.0,2024-01-05,1}
100.02024-01-023{100.0,2024-01-02,3}

value_counts gives a struct per distinct value, with the value and its count:

let basket = newDataFrame:
  fruit: ["cherry", "apple", "banana", "banana", "apple", "banana"]

nbShow basket
shape: (6, 1)
fruit
str
"cherry"
"apple"
"banana"
"banana"
"apple"
"banana"
nbShow basket.select(col.fruit.valueCounts(sort = true))
shape: (3, 1)
fruit
struct[2]
{"banana",3}
{"apple",2}
{"cherry",1}
nbShow basket.select(col.fruit.valueCounts(sort = true).struct.unnest)
shape: (3, 2)
fruitcount
stru32
"banana"3
"apple"2
"cherry"1
import nanuqbook
bookInit()

nbText: "# Textual and temporal data types"

attribution definitiveGuide(12, "Working with Textual, Temporal, and Nested Data Types",
  "The string, categorical, enum and temporal examples of the chapter; the data is read " &
  "from `data/definitive-guide/`. Left out: the string cache, `cat.get_categories` and " &
  "the deprecated physical ordering of categoricals.")

nbText: """
Beyond numbers, a column can hold text, a fixed set of categories, dates and times, or
nested values. Each of these types has its own namespace of expressions: `str`, `cat`,
`dt`, `list`, `arr` and `struct`. Nanuq has all six.

## String

### String Examples

Three messy lines of text:
"""

tabs """
import polars as pl

corpus = pl.DataFrame(
    {
        "raw_text": [
            "  Data Science is amazing ",
            "Data_analysis > Data entry",
            " Python&Polars; Fast",
        ]
    }
)

corpus
""":
  nbCode:
    import nanuq

    var corpus = newDataFrame:
      raw_text: ["  Data Science is amazing ", "Data_analysis > Data entry",
                 " Python&Polars; Fast"]

    nbShow corpus

nbText: "String expressions chain, each step working on the result of the one before:"

tabs """
corpus = corpus.with_columns(
    processed_text=pl.col("raw_text")
    .str.strip_chars()
    .str.to_lowercase()
    .str.replace_all("_", " ")
)
corpus
""":
  nbCode:
    corpus = corpus.withColumns(
      col.raw_text
        .str.stripChars
        .str.toLowercase
        .str.replaceAll("_", " ")
        .alias("processed_text"))
    nbShow corpus

tabs """
corpus.with_columns(
    first_5_chars=pl.col("processed_text").str.slice(0, 5),
    first_word=pl.col("processed_text")
    .str.split(" ")
    .list.get(0),
    second_word=pl.col("processed_text").str.split(" ").list.get(1),
)
""":
  nbCode:
    nbShow corpus.withColumns(
      col.processed_text.str.slice(0, 5).alias("first_5_chars"),
      col.processed_text
        .str.split(" ")
        .list.get(0)
        .alias("first_word"),
      col.processed_text.str.split(" ").list.get(1).alias("second_word"))

tabs """
corpus.with_columns(
    len_chars=pl.col("processed_text").str.len_chars(),
    len_bytes=pl.col("processed_text").str.len_bytes(),
    count_a=pl.col("processed_text").str.count_matches("a"),
)
""":
  nbCode:
    nbShow corpus.withColumns(
      col.processed_text.str.lenChars.alias("len_chars"),
      col.processed_text.str.lenBytes.alias("len_bytes"),
      col.processed_text.str.countMatches("a").alias("count_a"))

nbText: "Extracting all the hashtags of a post gives a list per row:"

tabs """
posts = pl.DataFrame(
    {"post": ["Loving #python and #polars!", "A boomer post without a hashtag"]}
)

hashtag_regex = r"#(\w+)"

posts.with_columns(
    hashtags=pl.col("post").str.extract_all(hashtag_regex)
)
""":
  nbCode:
    let posts = newDataFrame:
      post: ["Loving #python and #polars!", "A boomer post without a hashtag"]

    let hashtagRegex = r"#(\w+)"

    nbShow posts.withColumns(col.post.str.extractAll(hashtagRegex).alias("hashtags"))

nbText: """
## Categorical

A categorical column stores each distinct string once, and a small integer per row: its
physical representation.
"""

tabs """
cats = pl.DataFrame(
    {"name": ["Persian cat", "Siamese Cat", "Lynx", "Lynx"]},
    schema={"name": pl.Categorical},
)

cats.with_columns(name_physical=pl.col("name").to_physical())
""":
  nbCode:
    let cats = newDataFrame(
        series("name", ["Persian cat", "Siamese Cat", "Lynx", "Lynx"]))
      .cast({"name": Categorical})

    nbShow cats.withColumns(col.name.toPhysical.alias("name_physical"))

nanuq """
Nanuq builds the frame from strings and casts it, with a mapping from column names to
types, where Python passes a `schema`.
"""

nbText: """
### Categorical Examples

A second frame with some of the same cats. In recent versions of Polars all categoricals share one
global set of categories, so the same name has the same code in both frames:
"""

tabs """
more_cats = pl.DataFrame(
    {"name": ["Maine Coon Cat", "Lynx", "Lynx", "Siamese Cat"]},
    schema={"name": pl.Categorical},
)

more_cats.with_columns(name_physical=pl.col("name").to_physical())
""":
  nbCode:
    let moreCats = newDataFrame(
        series("name", ["Maine Coon Cat", "Lynx", "Lynx", "Siamese Cat"]))
      .cast({"name": Categorical})

    nbShow moreCats.withColumns(col.name.toPhysical.alias("name_physical"))

tabs """
cats.join(more_cats, on="name")
""":
  nbCode:
    nbShow cats.join(moreCats, on = ["name"])

nanuq """
The book's next cells wrap the frames in `pl.StringCache()`, which older versions of
Polars needed to join two categorical columns. It is a no-op now, and Nanuq has no string
cache: joins on categoricals just work. The book's `cat.get_categories` is gone from
Polars 2, in Python and in Nanuq.
"""

nbText: """
Sorting a categorical column sorts by the strings, not by the codes:
"""

tabs """
sorting_comparison_df = cats.select(cat_lexical=pl.col("name")).with_columns(
    cat_physical=pl.col("cat_lexical").to_physical()
)

sorting_comparison_df.sort(by="cat_lexical")
""":
  nbCode:
    let sortingComparisonDf = cats.select(col.name.alias("cat_lexical")).withColumns(
      col.cat_lexical.toPhysical.alias("cat_physical"))

    nbShow sortingComparisonDf.sort("cat_lexical")

nanuq """
The book compares physical with lexical ordering; physical ordering has been removed
from Polars since, so this cell sorts the frame directly instead of casting it to
`pl.Categorical("lexical")` first.
"""

nbText: """
## Enum

An `Enum` has a fixed list of categories, given up front, in order:
"""

tabs """
bear_enum_dtype = pl.Enum(["Polar", "Panda", "Brown"])

bear_enum_series = pl.Series(
    ["Polar", "Panda", "Brown", "Brown", "Polar"], dtype=bear_enum_dtype
)

bear_cat_series = pl.Series(
    ["Polar", "Panda", "Brown", "Brown", "Polar"], dtype=pl.Categorical
)

pl.DataFrame([bear_enum_series.alias("enum"), bear_cat_series.alias("cat")])
""":
  nbCode:
    type
      Bear = enum
        Polar, Panda, Brown

    let bearEnumSeries = series("enum", [Polar, Panda, Brown, Brown, Polar])

    let bearCatSeries = series("cat", ["Polar", "Panda", "Brown", "Brown", "Polar"])

    nbShow newDataFrame(bearEnumSeries, bearCatSeries).cast({"cat": Categorical})

nanuq """
A Nim enum is a Polars `Enum`: a Series of `Bear` values has the data type
`Enum(["Polar", "Panda", "Brown"])`. The book stops at the two Series; to show them,
both sides put them in one frame.
"""

nbText: """
## Temporal

### Temporal Examples

#### Loading from a CSV file

With `try_parse_dates`, the CSV reader recognises the dates:
"""

tabs """
pl.read_csv("data/definitive-guide/all_stocks.csv", try_parse_dates=True)
""":
  nbCode:
    nbShow readCsv("data/definitive-guide/all_stocks.csv", tryParseDates = true)

nbText: "#### Converting to and from a String"

tabs """
dates = pl.DataFrame({"date_str": ["2023-12-31", "2024-02-29"]}).with_columns(
    date=pl.col("date_str").str.to_date("%Y-%m-%d")
)

dates
""":
  nbCode:
    let dates = newDataFrame(series("date_str", ["2023-12-31", "2024-02-29"])).withColumns(
      col.date_str.str.toDate("%Y-%m-%d").alias("date"))

    nbShow dates

tabs """
dates.with_columns(formatted_date=pl.col("date").dt.to_string("%d-%m-%Y"))
""":
  nbCode:
    nbShow dates.withColumns(col.date.dt.toString("%d-%m-%Y").alias("formatted_date"))

nanuq """
`col("date")` with a string: `col.date` would be the `date` proc, Nanuq's date literal.
"""

nbText: "#### Generating date ranges"

tabs """
pl.DataFrame(
    {
        "monday": pl.date_range(
            start=pl.date(2024, 10, 28),
            end=pl.date(2024, 12, 1),
            interval="1w",
            eager=True,
        ),
    }
)
""":
  nbCode:
    nbShow newDataFrame().select(
      dateRange(start = date(2024, 10, 28), stop = date(2024, 12, 1), interval = 1.weeks)
        .alias("monday"))

nanuq """
`dateRange` is an expression: Nanuq has no eager version, so it is selected from an
empty frame. The interval can be a `std/times` interval, `1.weeks`, or Polars' `"1w"`;
the end is `stop`, since `end` is a Nim keyword.
"""

nbText: """
#### Time zones

Strings with mixed UTC offsets parse into one UTC column, which can then be shown in a
time zone:
"""

tabs """
pl.DataFrame(
    {
        "utc_mixed_offset": [
            "2021-03-27T00:00:00+0100",
            "2021-03-28T00:00:00+0100",
            "2021-03-29T00:00:00+0200",
            "2021-03-30T00:00:00+0200",
        ]
    }
).with_columns(
    parsed=pl.col("utc_mixed_offset").str.to_datetime(
        "%Y-%m-%dT%H:%M:%S%z"
    )
).with_columns(
    converted=pl.col("parsed").dt.convert_time_zone("Europe/Amsterdam")
)
""":
  nbCode:
    nbShow newDataFrame(series("utc_mixed_offset", [
        "2021-03-27T00:00:00+0100",
        "2021-03-28T00:00:00+0100",
        "2021-03-29T00:00:00+0200",
        "2021-03-30T00:00:00+0200"]))
      .withColumns(
        col.utc_mixed_offset.str.toDatetime("%Y-%m-%dT%H:%M:%S%z").alias("parsed"))
      .withColumns(
        col.parsed.dt.convertTimeZone("Europe/Amsterdam").alias("converted"))

nbText: """
## List

### List Examples

A list column holds a list of values of the same type in each row, of any length. Some
list methods reduce each list to one value:
"""

tabs """
bools = pl.DataFrame({"values": [[True, True], [False, False, True], [False]]})

bools.with_columns(
    all_true=pl.col("values").list.all(),
    any_true=pl.col("values").list.any(),
)
""":
  nbCode:
    let bools = newDataFrame:
      values: [@[true, true], @[false, false, true], @[false]]

    nbShow bools.withColumns(
      col.values.list.all.alias("all_true"),
      col.values.list.any.alias("any_true"))

nbText: "`list.eval` runs any expression on the values of each list, with `pl.element()` standing for them:"

tabs """
groups = pl.DataFrame({"ages": [[18, 21], [30, 40, 50], [42, 69]]})

groups.with_columns(
    over_forty=pl.col("ages").list.eval(
        pl.element() > 40,
        parallel=True,
    )
).with_columns(
    all_over_forty=pl.col("over_forty").list.all()
)
""":
  nbCode:
    let groups = newDataFrame:
      ages: [@[18, 21], @[30, 40, 50], @[42, 69]]

    nbShow groups.withColumns(
      col.ages.list.eval(element() > 40).alias("over_forty")
    ).withColumns(
      col.over_forty.list.all.alias("all_over_forty"))

tabs """
groups.with_columns(
    ages_sorted_descending=pl.col("ages").list.sort(descending=True)
)
""":
  nbCode:
    nbShow groups.withColumns(
      col.ages.list.sort(descending = true).alias("ages_sorted_descending"))

nbText: "Exploding gives one row per value, on the frame or on one column:"

tabs """
groups.explode("ages")
""":
  nbCode:
    nbShow groups.explode("ages")

tabs """
groups.select(ages=pl.col("ages").list.explode())
""":
  nbCode:
    nbShow groups.select(col.ages.list.explode.alias("ages"))

nbText: """
## Array

### Array Examples

An array column holds the same number of values in every row, like a week of
temperatures:
"""

tabs """
events = pl.DataFrame(
    [
        pl.Series(
            "location", ["Paris", "Amsterdam", "Barcelona"], dtype=pl.String
        ),
        pl.Series(
            "temperatures",
            [
                [23, 27, 21, 22, 24, 23, 22],
                [17, 19, 15, 22, 18, 20, 21],
                [30, 32, 28, 29, 34, 33, 31],
            ],
            dtype=pl.Array(pl.Int64, shape=7),
        ),
    ]
)

events
""":
  nbCode:
    let events = newDataFrame:
      location: ["Paris", "Amsterdam", "Barcelona"]
      temperatures: [[23, 27, 21, 22, 24, 23, 22],
                     [17, 19, 15, 22, 18, 20, 21],
                     [30, 32, 28, 29, 34, 33, 31]]

    nbShow events

nanuq """
No `dtype` needed: a Nim `array[7, int]` is an `Array(Int64, 7)` already.
"""

tabs """
events.with_columns(
    median=pl.col("temperatures").arr.median(),
    max=pl.col("temperatures").arr.max(),
    warmest_dow=pl.col("temperatures").arr.arg_max(),
)
""":
  nbCode:
    nbShow events.withColumns(
      col.temperatures.arr.median.alias("median"),
      col.temperatures.arr.max.alias("max"),
      col.temperatures.arr.argMax.alias("warmest_dow"))

nbText: """
## Struct

### Struct Examples

A struct column holds a few named fields in each row:
"""

tabs """
from datetime import date

orders = pl.DataFrame(
    {
        "customer_id": [2781, 6139, 5392],
        "order_details": [
            {"amount": 250.00, "date": date(2024, 1, 3), "items": 5},
            {"amount": 150.00, "date": date(2024, 1, 5), "items": 1},
            {"amount": 100.00, "date": date(2024, 1, 2), "items": 3},
        ],
    },
)

orders
""":
  nbCode:
    let orders = newDataFrame:
      customer_id: [2781, 6139, 5392]
      order_details: [
        (amount: 250.00, date: date(2024, 1, 3), items: 5),
        (amount: 150.00, date: date(2024, 1, 5), items: 1),
        (amount: 100.00, date: date(2024, 1, 2), items: 3)]

    nbShow orders

nanuq """
A Python dict is a named tuple in Nim; Nanuq's `date(y, m, d)` gives a Polars `Date`.
"""

tabs """
orders.select(pl.col("order_details").struct.field("amount"))
""":
  nbCode:
    nbShow orders.select(col.order_details.struct.field("amount"))

tabs """
order_details_df = orders.unnest("order_details")

order_details_df
""":
  nbCode:
    let orderDetails = orders.unnest("order_details")

    nbShow orderDetails

tabs """
order_details_df.select(
    "amount",
    "date",
    "items",
    order_details=pl.struct(pl.col("amount"), pl.col("date"), pl.col("items")),
)
""":
  nbCode:
    nbShow orderDetails.select(
      col.amount,
      col.date,
      col.items,
      asStruct(col.amount, col.date, col.items).alias("order_details"))

nanuq """
`pl.struct` is `asStruct` in Nanuq: `struct` is the namespace.
"""

nbText: "`value_counts` gives a struct per distinct value, with the value and its count:"

tabs """
basket = pl.DataFrame(
    {
        "fruit": ["cherry", "apple", "banana", "banana", "apple", "banana"],
    }
)

basket
""":
  nbCode:
    let basket = newDataFrame:
      fruit: ["cherry", "apple", "banana", "banana", "apple", "banana"]

    nbShow basket

tabs """
basket.select(pl.col("fruit").value_counts(sort=True))
""":
  nbCode:
    nbShow basket.select(col.fruit.valueCounts(sort = true))

tabs """
basket.select(pl.col("fruit").value_counts(sort=True).struct.unnest())
""":
  nbCode:
    nbShow basket.select(col.fruit.valueCounts(sort = true).struct.unnest)

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