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
| raw_text |
|---|
| str |
| " Data Science is amazing " |
| "Data_analysis > Data entry" |
| " Python&Polars; Fast" |
import polars as pl
corpus = pl.DataFrame(
{
"raw_text": [
" Data Science is amazing ",
"Data_analysis > Data entry",
" Python&Polars; Fast",
]
}
)
corpus| 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
| raw_text | processed_text |
|---|---|
| str | str |
| " Data Science is amazing " | "data science is amazing" |
| "Data_analysis > Data entry" | "data analysis > data entry" |
| " Python&Polars; Fast" | "python&polars; fast" |
corpus = corpus.with_columns(
processed_text=pl.col("raw_text")
.str.strip_chars()
.str.to_lowercase()
.str.replace_all("_", " ")
)
corpus| raw_text | processed_text |
|---|---|
| str | str |
| " 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"))
| raw_text | processed_text | first_5_chars | first_word | second_word |
|---|---|---|---|---|
| str | str | str | str | str |
| " 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" |
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),
)| raw_text | processed_text | first_5_chars | first_word | second_word |
|---|---|---|---|---|
| str | str | str | str | str |
| " 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"))
| raw_text | processed_text | len_chars | len_bytes | count_a |
|---|---|---|---|---|
| str | str | u32 | u32 | u32 |
| " Data Science is amazing " | "data science is amazing" | 23 | 23 | 4 |
| "Data_analysis > Data entry" | "data analysis > data entry" | 26 | 26 | 6 |
| " Python&Polars; Fast" | "python&polars; fast" | 19 | 19 | 2 |
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"),
)| raw_text | processed_text | len_chars | len_bytes | count_a |
|---|---|---|---|---|
| str | str | u32 | u32 | u32 |
| " Data Science is amazing " | "data science is amazing" | 23 | 23 | 4 |
| "Data_analysis > Data entry" | "data analysis > data entry" | 26 | 26 | 6 |
| " Python&Polars; Fast" | "python&polars; fast" | 19 | 19 | 2 |
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"))
| post | hashtags |
|---|---|
| str | list[str] |
| "Loving #python and #polars!" | ["#python", "#polars"] |
| "A boomer post without a hashta⦠| [] |
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)
)| post | hashtags |
|---|---|
| str | list[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"))
| name | name_physical |
|---|---|
| cat | u32 |
| "Persian cat" | 0 |
| "Siamese Cat" | 1 |
| "Lynx" | 2 |
| "Lynx" | 2 |
cats = pl.DataFrame(
{"name": ["Persian cat", "Siamese Cat", "Lynx", "Lynx"]},
schema={"name": pl.Categorical},
)
cats.with_columns(name_physical=pl.col("name").to_physical())| name | name_physical |
|---|---|
| cat | u32 |
| "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"))
| name | name_physical |
|---|---|
| cat | u32 |
| "Maine Coon Cat" | 3 |
| "Lynx" | 2 |
| "Lynx" | 2 |
| "Siamese Cat" | 1 |
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())| name | name_physical |
|---|---|
| cat | u32 |
| "Maine Coon Cat" | 3 |
| "Lynx" | 2 |
| "Lynx" | 2 |
| "Siamese Cat" | 1 |
nbShow cats.join(moreCats, on = ["name"])
| name |
|---|
| cat |
| "Lynx" |
| "Lynx" |
| "Lynx" |
| "Lynx" |
| "Siamese Cat" |
cats.join(more_cats, on="name")| 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")
| cat_lexical | cat_physical |
|---|---|
| cat | u32 |
| "Lynx" | 2 |
| "Lynx" | 2 |
| "Persian cat" | 0 |
| "Siamese Cat" | 1 |
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")| cat_lexical | cat_physical |
|---|---|
| cat | u32 |
| "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})
| enum | cat |
|---|---|
| enum | cat |
| "Polar" | "Polar" |
| "Panda" | "Panda" |
| "Brown" | "Brown" |
| "Brown" | "Brown" |
| "Polar" | "Polar" |
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")])| enum | cat |
|---|---|
| enum | cat |
| "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)
| symbol | date | open | high | low | close | adj close | volume |
|---|---|---|---|---|---|---|---|
| str | date | f64 | f64 | f64 | f64 | f64 | i64 |
| "ASML" | 1999-01-04 | 11.765625 | 12.28125 | 11.765625 | 12.140625 | 7.522523 | 1801867 |
| "ASML" | 1999-01-05 | 11.859375 | 14.25 | 11.71875 | 13.96875 | 8.655257 | 8241600 |
| "ASML" | 1999-01-06 | 14.25 | 17.601563 | 14.203125 | 16.875 | 10.456018 | 16400267 |
| "ASML" | 1999-01-07 | 14.742188 | 17.8125 | 14.53125 | 16.851563 | 10.441495 | 17722133 |
| "ASML" | 1999-01-08 | 16.078125 | 16.289063 | 15.023438 | 15.796875 | 9.787995 | 10696000 |
| … | … | … | … | … | … | … | … |
| "TSM" | 2023-06-26 | 102.019997 | 103.040001 | 100.089996 | 100.110001 | 99.125954 | 8560000 |
| "TSM" | 2023-06-27 | 101.150002 | 102.790001 | 100.019997 | 102.080002 | 101.076591 | 9732000 |
| "TSM" | 2023-06-28 | 100.5 | 101.879997 | 100.220001 | 100.919998 | 99.927986 | 8160900 |
| "TSM" | 2023-06-29 | 101.339996 | 101.519997 | 100.019997 | 100.639999 | 99.650742 | 7383900 |
| "TSM" | 2023-06-30 | 101.400002 | 101.889999 | 100.410004 | 100.919998 | 99.927986 | 11701700 |
pl.read_csv("data/definitive-guide/all_stocks.csv", try_parse_dates=True)| symbol | date | open | high | low | close | adj close | volume |
|---|---|---|---|---|---|---|---|
| str | date | f64 | f64 | f64 | f64 | f64 | i64 |
| "ASML" | 1999-01-04 | 11.765625 | 12.28125 | 11.765625 | 12.140625 | 7.522523 | 1801867 |
| "ASML" | 1999-01-05 | 11.859375 | 14.25 | 11.71875 | 13.96875 | 8.655257 | 8241600 |
| "ASML" | 1999-01-06 | 14.25 | 17.601563 | 14.203125 | 16.875 | 10.456018 | 16400267 |
| "ASML" | 1999-01-07 | 14.742188 | 17.8125 | 14.53125 | 16.851563 | 10.441495 | 17722133 |
| "ASML" | 1999-01-08 | 16.078125 | 16.289063 | 15.023438 | 15.796875 | 9.787995 | 10696000 |
| … | … | … | … | … | … | … | … |
| "TSM" | 2023-06-26 | 102.019997 | 103.040001 | 100.089996 | 100.110001 | 99.125954 | 8560000 |
| "TSM" | 2023-06-27 | 101.150002 | 102.790001 | 100.019997 | 102.080002 | 101.076591 | 9732000 |
| "TSM" | 2023-06-28 | 100.5 | 101.879997 | 100.220001 | 100.919998 | 99.927986 | 8160900 |
| "TSM" | 2023-06-29 | 101.339996 | 101.519997 | 100.019997 | 100.639999 | 99.650742 | 7383900 |
| "TSM" | 2023-06-30 | 101.400002 | 101.889999 | 100.410004 | 100.919998 | 99.927986 | 11701700 |
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
| date_str | date |
|---|---|
| str | date |
| "2023-12-31" | 2023-12-31 |
| "2024-02-29" | 2024-02-29 |
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| date_str | date |
|---|---|
| str | date |
| "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"))
| date_str | date | formatted_date |
|---|---|---|
| str | date | str |
| "2023-12-31" | 2023-12-31 | "31-12-2023" |
| "2024-02-29" | 2024-02-29 | "29-02-2024" |
dates.with_columns(formatted_date=pl.col("date").dt.to_string("%d-%m-%Y"))| date_str | date | formatted_date |
|---|---|---|
| str | date | str |
| "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"))
| monday |
|---|
| date |
| 2024-10-28 |
| 2024-11-04 |
| 2024-11-11 |
| 2024-11-18 |
| 2024-11-25 |
pl.DataFrame(
{
"monday": pl.date_range(
start=pl.date(2024, 10, 28),
end=pl.date(2024, 12, 1),
interval="1w",
eager=True,
),
}
)| 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"))
| utc_mixed_offset | parsed | converted |
|---|---|---|
| str | datetime[ΞΌs, UTC] | datetime[ΞΌs, Europe/Amsterdam] |
| "2021-03-27T00:00:00+0100" | 2021-03-26 23:00:00 UTC | 2021-03-27 00:00:00 CET |
| "2021-03-28T00:00:00+0100" | 2021-03-27 23:00:00 UTC | 2021-03-28 00:00:00 CET |
| "2021-03-29T00:00:00+0200" | 2021-03-28 22:00:00 UTC | 2021-03-29 00:00:00 CEST |
| "2021-03-30T00:00:00+0200" | 2021-03-29 22:00:00 UTC | 2021-03-30 00:00:00 CEST |
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")
)| utc_mixed_offset | parsed | converted |
|---|---|---|
| str | datetime[ΞΌs, UTC] | datetime[ΞΌs, Europe/Amsterdam] |
| "2021-03-27T00:00:00+0100" | 2021-03-26 23:00:00 UTC | 2021-03-27 00:00:00 CET |
| "2021-03-28T00:00:00+0100" | 2021-03-27 23:00:00 UTC | 2021-03-28 00:00:00 CET |
| "2021-03-29T00:00:00+0200" | 2021-03-28 22:00:00 UTC | 2021-03-29 00:00:00 CEST |
| "2021-03-30T00:00:00+0200" | 2021-03-29 22:00:00 UTC | 2021-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"))
| values | all_true | any_true |
|---|---|---|
| list[bool] | bool | bool |
| [true, true] | true | true |
| [false, false, true] | false | true |
| [false] | false | false |
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(),
)| values | all_true | any_true |
|---|---|---|
| list[bool] | bool | bool |
| [true, true] | true | true |
| [false, false, true] | false | true |
| [false] | false | false |
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"))
| ages | over_forty | all_over_forty |
|---|---|---|
| list[i64] | list[bool] | bool |
| [18, 21] | [false, false] | false |
| [30, 40, 50] | [false, false, true] | false |
| [42, 69] | [true, true] | true |
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()
)| ages | over_forty | all_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"))
| ages | ages_sorted_descending |
|---|---|
| list[i64] | list[i64] |
| [18, 21] | [21, 18] |
| [30, 40, 50] | [50, 40, 30] |
| [42, 69] | [69, 42] |
groups.with_columns(
ages_sorted_descending=pl.col("ages").list.sort(descending=True)
)| ages | ages_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")
| ages |
|---|
| i64 |
| 18 |
| 21 |
| 30 |
| 40 |
| 50 |
| 42 |
| 69 |
groups.explode("ages")| ages |
|---|
| i64 |
| 18 |
| 21 |
| 30 |
| 40 |
| 50 |
| 42 |
| 69 |
nbShow groups.select(col.ages.list.explode.alias("ages"))
| ages |
|---|
| i64 |
| 18 |
| 21 |
| 30 |
| 40 |
| 50 |
| 42 |
| 69 |
groups.select(ages=pl.col("ages").list.explode())| 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
| location | temperatures |
|---|---|
| str | array[i64, 7] |
| "Paris" | [23, 27, β¦ 22] |
| "Amsterdam" | [17, 19, β¦ 21] |
| "Barcelona" | [30, 32, β¦ 31] |
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| location | temperatures |
|---|---|
| str | array[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"))
| location | temperatures | median | max | warmest_dow |
|---|---|---|---|---|
| str | array[i64, 7] | f64 | i64 | u32 |
| "Paris" | [23, 27, β¦ 22] | 23.0 | 27 | 1 |
| "Amsterdam" | [17, 19, β¦ 21] | 19.0 | 22 | 3 |
| "Barcelona" | [30, 32, β¦ 31] | 31.0 | 34 | 4 |
events.with_columns(
median=pl.col("temperatures").arr.median(),
max=pl.col("temperatures").arr.max(),
warmest_dow=pl.col("temperatures").arr.arg_max(),
)| location | temperatures | median | max | warmest_dow |
|---|---|---|---|---|
| str | array[i64, 7] | f64 | i64 | u32 |
| "Paris" | [23, 27, β¦ 22] | 23.0 | 27 | 1 |
| "Amsterdam" | [17, 19, β¦ 21] | 19.0 | 22 | 3 |
| "Barcelona" | [30, 32, β¦ 31] | 31.0 | 34 | 4 |
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
| customer_id | order_details |
|---|---|
| i64 | struct[3] |
| 2781 | {250.0,2024-01-03,5} |
| 6139 | {150.0,2024-01-05,1} |
| 5392 | {100.0,2024-01-02,3} |
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| customer_id | order_details |
|---|---|
| i64 | struct[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"))
| amount |
|---|
| f64 |
| 250.0 |
| 150.0 |
| 100.0 |
orders.select(pl.col("order_details").struct.field("amount"))| amount |
|---|
| f64 |
| 250.0 |
| 150.0 |
| 100.0 |
let orderDetails = orders.unnest("order_details")
nbShow orderDetails
| customer_id | amount | date | items |
|---|---|---|---|
| i64 | f64 | date | i64 |
| 2781 | 250.0 | 2024-01-03 | 5 |
| 6139 | 150.0 | 2024-01-05 | 1 |
| 5392 | 100.0 | 2024-01-02 | 3 |
order_details_df = orders.unnest("order_details")
order_details_df| customer_id | amount | date | items |
|---|---|---|---|
| i64 | f64 | date | i64 |
| 2781 | 250.0 | 2024-01-03 | 5 |
| 6139 | 150.0 | 2024-01-05 | 1 |
| 5392 | 100.0 | 2024-01-02 | 3 |
nbShow orderDetails.select(
col.amount,
col.date,
col.items,
asStruct(col.amount, col.date, col.items).alias("order_details"))
| amount | date | items | order_details |
|---|---|---|---|
| f64 | date | i64 | struct[3] |
| 250.0 | 2024-01-03 | 5 | {250.0,2024-01-03,5} |
| 150.0 | 2024-01-05 | 1 | {150.0,2024-01-05,1} |
| 100.0 | 2024-01-02 | 3 | {100.0,2024-01-02,3} |
order_details_df.select(
"amount",
"date",
"items",
order_details=pl.struct(pl.col("amount"), pl.col("date"), pl.col("items")),
)| amount | date | items | order_details |
|---|---|---|---|
| f64 | date | i64 | struct[3] |
| 250.0 | 2024-01-03 | 5 | {250.0,2024-01-03,5} |
| 150.0 | 2024-01-05 | 1 | {150.0,2024-01-05,1} |
| 100.0 | 2024-01-02 | 3 | {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
| fruit |
|---|
| str |
| "cherry" |
| "apple" |
| "banana" |
| "banana" |
| "apple" |
| "banana" |
basket = pl.DataFrame(
{
"fruit": ["cherry", "apple", "banana", "banana", "apple", "banana"],
}
)
basket| fruit |
|---|
| str |
| "cherry" |
| "apple" |
| "banana" |
| "banana" |
| "apple" |
| "banana" |
nbShow basket.select(col.fruit.valueCounts(sort = true))
| fruit |
|---|
| struct[2] |
| {"banana",3} |
| {"apple",2} |
| {"cherry",1} |
basket.select(pl.col("fruit").value_counts(sort=True))| fruit |
|---|
| struct[2] |
| {"banana",3} |
| {"apple",2} |
| {"cherry",1} |
nbShow basket.select(col.fruit.valueCounts(sort = true).struct.unnest)
| fruit | count |
|---|---|
| str | u32 |
| "banana" | 3 |
| "apple" | 2 |
| "cherry" | 1 |
basket.select(pl.col("fruit").value_counts(sort=True).struct.unnest())| fruit | count |
|---|---|
| str | u32 |
| "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()
On GitHub: book/definitive-guide/textual_temporal.nim