Time series: grouping
Grouping by fixed windows
We can calculate temporal statistics using group_by_dynamic to group rows into
days/months/years etc.
Annual average example
In following simple example we calculate the annual average closing price of Apple stock prices. We first load the data from CSV:
import std/times
import nanuq
var df = readCsv("data/polars/apple_stock.csv", tryParseDates = true)
df = df.sort("Date")
nbShow df
| Date | Close |
|---|---|
| date | f64 |
| 1981-02-23 | 24.62 |
| 1981-05-06 | 27.38 |
| 1981-05-18 | 28.0 |
| 1981-09-25 | 14.25 |
| 1982-07-08 | 11.0 |
| … | … |
| 2012-05-16 | 546.08 |
| 2012-12-04 | 575.85 |
| 2013-07-05 | 417.42 |
| 2013-11-07 | 512.49 |
| 2014-02-25 | 522.06 |
from datetime import date, datetime
import polars as pl
df = pl.read_csv("data/polars/apple_stock.csv", try_parse_dates=True)
df = df.sort("Date")
df| Date | Close |
|---|---|
| date | f64 |
| 1981-02-23 | 24.62 |
| 1981-05-06 | 27.38 |
| 1981-05-18 | 28.0 |
| 1981-09-25 | 14.25 |
| 1982-07-08 | 11.0 |
| … | … |
| 2012-05-16 | 546.08 |
| 2012-12-04 | 575.85 |
| 2013-07-05 | 417.42 |
| 2013-11-07 | 512.49 |
| 2014-02-25 | 522.06 |
Info. The dates are sorted in ascending order - if they are not sorted in this way the
group_by_dynamicoutput will not be correct!
To get the annual average closing price we tell group_by_dynamic that we want to:
- group by the
Datecolumn on an annual (1y) basis - take the mean values of the
Closecolumn for each year:
The annual average closing price is then:
let annualAverageDf = df.groupByDynamic("Date", every = "1y")
.agg(col.Close.mean)
let dfWithYear = annualAverageDf.withColumns(col("Date").dt.year.alias("year"))
nbShow dfWithYear
| Date | Close | year |
|---|---|---|
| date | f64 | i32 |
| 1981-01-01 | 23.5625 | 1981 |
| 1982-01-01 | 11.0 | 1982 |
| 1983-01-01 | 30.543333 | 1983 |
| 1984-01-01 | 27.583333 | 1984 |
| 1985-01-01 | 18.166667 | 1985 |
| … | … | … |
| 2010-01-01 | 278.265 | 2010 |
| 2011-01-01 | 368.225 | 2011 |
| 2012-01-01 | 560.965 | 2012 |
| 2013-01-01 | 464.955 | 2013 |
| 2014-01-01 | 522.06 | 2014 |
annual_average_df = df.group_by_dynamic("Date", every="1y").agg(pl.col("Close").mean())
df_with_year = annual_average_df.with_columns(pl.col("Date").dt.year().alias("year"))
df_with_year| Date | Close | year |
|---|---|---|
| date | f64 | i32 |
| 1981-01-01 | 23.5625 | 1981 |
| 1982-01-01 | 11.0 | 1982 |
| 1983-01-01 | 30.543333 | 1983 |
| 1984-01-01 | 27.583333 | 1984 |
| 1985-01-01 | 18.166667 | 1985 |
| … | … | … |
| 2010-01-01 | 278.265 | 2010 |
| 2011-01-01 | 368.225 | 2011 |
| 2012-01-01 | 560.965 | 2012 |
| 2013-01-01 | 464.955 | 2013 |
| 2014-01-01 | 522.06 | 2014 |
Parameters for group_by_dynamic
A dynamic window is defined by a:
- every: indicates the interval of the window
- period: indicates the duration of the window
- offset: can be used to offset the start of the windows
The value for every sets how often the groups start. The time period values are flexible
-
for example we could take:
-
the average over 2 year intervals by replacing
1ywith2y -
the average over 18 month periods by replacing
1ywith1y6mo
We can also use the period parameter to set how long the time period for each group is.
For example, if we set the every parameter to be 1y and the period parameter to be
2y then we would get groups at one year intervals where each groups spanned two years.
If the period parameter is not specified then it is set equal to the every parameter
so that if the every parameter is set to be 1y then each group spans 1y as well.
Because every does not have to be equal to period, we can create many groups in a very flexible way. They may overlap or leave boundaries between them.
Let's see how the windows for some parameter combinations would look. Let's start out boring. π₯±
- every: 1 day ->
"1d" - period: 1 day ->
"1d"
this creates adjacent windows of the same size
|--|
|--|
|--|
- every: 1 day ->
"1d" - period: 2 days ->
"2d"
these windows have an overlap of 1 day
|----|
|----|
|----|
- every: 2 days ->
"2d" - period: 1 day ->
"1d"
this would leave gaps between the windows
data points that in these gaps will not be a member of any group
|--|
|--|
|--|
truncate
The truncate parameter is a Boolean variable that determines what datetime value is
associated with each group in the output. In the example above the first data point is on
23rd February 1981. If truncate = True (the default) then the date for the first year in
the annual average is 1st January 1981. However, if truncate = False then the date for
the first year in the annual average is the date of the first data point on 23rd February
1981. Note that truncate only affects what's shown in the Date column and does not
affect the window boundaries.
Using expressions in group_by_dynamic
We aren't restricted to using simple aggregations like mean in a group by operation - we
can use the full range of expressions available in Polars.
In the snippet below we create a date range with every day ("1d") in 2021 and turn
this into a DataFrame.
Then in the group_by_dynamic we create dynamic windows that start every month
("1mo") and have a window length of 1 month. The values that match these dynamic
windows are then assigned to that group and can be aggregated with the powerful expression
API.
Below we show an example where we use group_by_dynamic to compute:
- the number of days until the end of the month
- the number of days in a month
let days = newDataFrame().select(
dateRange(start = date(2021, 1, 1), stop = date(2021, 12, 31), interval = "1d")
.alias("time"))
nbShow days.groupByDynamic("time", every = "1mo", period = "1mo",
closed = ClosedWindow.Left).agg(
col.time.cumCount.reverse.head(3).alias("day/eom"),
((col.time - col.time.first).last.dt.totalDays + 1).alias("days_in_month"))
| time | day/eom | days_in_month |
|---|---|---|
| date | list[u32] | i64 |
| 2021-01-01 | [31, 30, 29] | 31 |
| 2021-02-01 | [28, 27, 26] | 28 |
| 2021-03-01 | [31, 30, 29] | 31 |
| 2021-04-01 | [30, 29, 28] | 30 |
| 2021-05-01 | [31, 30, 29] | 31 |
| … | … | … |
| 2021-08-01 | [31, 30, 29] | 31 |
| 2021-09-01 | [30, 29, 28] | 30 |
| 2021-10-01 | [31, 30, 29] | 31 |
| 2021-11-01 | [30, 29, 28] | 30 |
| 2021-12-01 | [31, 30, 29] | 31 |
df = (
pl.date_range(
start=date(2021, 1, 1),
end=date(2021, 12, 31),
interval="1d",
eager=True,
)
.alias("time")
.to_frame()
)
df.group_by_dynamic("time", every="1mo", period="1mo", closed="left").agg(
pl.col("time").cum_count().reverse().head(3).alias("day/eom"),
((pl.col("time") - pl.col("time").first()).last().dt.total_days() + 1).alias(
"days_in_month"
),
)| time | day/eom | days_in_month |
|---|---|---|
| date | list[u32] | i64 |
| 2021-01-01 | [31, 30, 29] | 31 |
| 2021-02-01 | [28, 27, 26] | 28 |
| 2021-03-01 | [31, 30, 29] | 31 |
| 2021-04-01 | [30, 29, 28] | 30 |
| 2021-05-01 | [31, 30, 29] | 31 |
| … | … | … |
| 2021-08-01 | [31, 30, 29] | 31 |
| 2021-09-01 | [30, 29, 28] | 30 |
| 2021-10-01 | [31, 30, 29] | 31 |
| 2021-11-01 | [30, 29, 28] | 30 |
| 2021-12-01 | [31, 30, 29] | 31 |
Grouping by rolling windows
The rolling operation, rolling, is another entrance to the group_by/agg context. But
different from the group_by_dynamic where the windows are fixed by a parameter every
and period. In a rolling, the windows are not fixed at all! They are determined by the
values in the index_column.
So imagine having a time column with the values {2021-01-06, 2021-01-10} and a
period="5d" this would create the following windows:
2021-01-01 2021-01-06
|----------|
2021-01-05 2021-01-10
|----------|
Because the windows of a rolling group by are always determined by the values in the
DataFrame column, the number of groups is always equal to the original DataFrame.
Combining group by operations
Rolling and dynamic group by operations can be combined with normal group by operations.
Below is an example with a dynamic group by.
let df2 = newDataFrame().select(
datetimeRange(start = dateTime(2021, mDec, 16, zone = utc()),
stop = dateTime(2021, mDec, 16, 3, zone = utc()),
interval = "30m").alias("time")
).withColumns(series("groups", ["a", "a", "a", "b", "b", "a", "a"]))
nbShow df2
| time | groups |
|---|---|
| datetime[ΞΌs] | str |
| 2021-12-16 00:00:00 | "a" |
| 2021-12-16 00:30:00 | "a" |
| 2021-12-16 01:00:00 | "a" |
| 2021-12-16 01:30:00 | "b" |
| 2021-12-16 02:00:00 | "b" |
| 2021-12-16 02:30:00 | "a" |
| 2021-12-16 03:00:00 | "a" |
df = pl.DataFrame(
{
"time": pl.datetime_range(
start=datetime(2021, 12, 16),
end=datetime(2021, 12, 16, 3),
interval="30m",
eager=True,
),
"groups": ["a", "a", "a", "b", "b", "a", "a"],
}
)
df| time | groups |
|---|---|
| datetime[ΞΌs] | str |
| 2021-12-16 00:00:00 | "a" |
| 2021-12-16 00:30:00 | "a" |
| 2021-12-16 01:00:00 | "a" |
| 2021-12-16 01:30:00 | "b" |
| 2021-12-16 02:00:00 | "b" |
| 2021-12-16 02:30:00 | "a" |
| 2021-12-16 03:00:00 | "a" |
let byHour = df2.groupByDynamic(
"time",
every = "1h",
closed = ClosedWindow.Both,
groupBy = [col.groups],
includeBoundaries = true,
).agg(len())
nbShow byHour
| groups | _lower_boundary | _upper_boundary | time | len |
|---|---|---|---|---|
| str | datetime[ΞΌs] | datetime[ΞΌs] | datetime[ΞΌs] | u32 |
| "a" | 2021-12-16 00:00:00 | 2021-12-16 01:00:00 | 2021-12-16 00:00:00 | 3 |
| "a" | 2021-12-16 01:00:00 | 2021-12-16 02:00:00 | 2021-12-16 01:00:00 | 1 |
| "a" | 2021-12-16 02:00:00 | 2021-12-16 03:00:00 | 2021-12-16 02:00:00 | 2 |
| "a" | 2021-12-16 03:00:00 | 2021-12-16 04:00:00 | 2021-12-16 03:00:00 | 1 |
| "b" | 2021-12-16 01:00:00 | 2021-12-16 02:00:00 | 2021-12-16 01:00:00 | 2 |
| "b" | 2021-12-16 02:00:00 | 2021-12-16 03:00:00 | 2021-12-16 02:00:00 | 1 |
df.group_by_dynamic(
"time",
every="1h",
closed="both",
group_by="groups",
include_boundaries=True,
).agg(pl.len())| groups | _lower_boundary | _upper_boundary | time | len |
|---|---|---|---|---|
| str | datetime[ΞΌs] | datetime[ΞΌs] | datetime[ΞΌs] | u32 |
| "a" | 2021-12-16 00:00:00 | 2021-12-16 01:00:00 | 2021-12-16 00:00:00 | 3 |
| "a" | 2021-12-16 01:00:00 | 2021-12-16 02:00:00 | 2021-12-16 01:00:00 | 1 |
| "a" | 2021-12-16 02:00:00 | 2021-12-16 03:00:00 | 2021-12-16 02:00:00 | 2 |
| "a" | 2021-12-16 03:00:00 | 2021-12-16 04:00:00 | 2021-12-16 03:00:00 | 1 |
| "b" | 2021-12-16 01:00:00 | 2021-12-16 02:00:00 | 2021-12-16 01:00:00 | 2 |
| "b" | 2021-12-16 02:00:00 | 2021-12-16 03:00:00 | 2021-12-16 02:00:00 | 1 |
import nanuqbook
bookInit()
nbText: "# Time series: grouping"
attribution userGuide("Grouping", "transformations/time-series/rolling",
"The data is read from `data/polars/apple_stock.csv`.")
nbText: """
## Grouping by fixed windows
We can calculate temporal statistics using `group_by_dynamic` to group rows into
days/months/years etc.
### Annual average example
In following simple example we calculate the annual average closing price of Apple stock
prices. We first load the data from CSV:
"""
tabs """
from datetime import date, datetime
import polars as pl
df = pl.read_csv("data/polars/apple_stock.csv", try_parse_dates=True)
df = df.sort("Date")
df
""":
nbCode:
import std/times
import nanuq
var df = readCsv("data/polars/apple_stock.csv", tryParseDates = true)
df = df.sort("Date")
nbShow df
nbText: """
> **Info.** The dates are sorted in ascending order - if they are not sorted in this way
> the `group_by_dynamic` output will not be correct!
To get the annual average closing price we tell `group_by_dynamic` that we want to:
- group by the `Date` column on an annual (`1y`) basis
- take the mean values of the `Close` column for each year:
The annual average closing price is then:
"""
nanuq """
In Nanuq `every` is a Polars interval (`"1y"`) or a std/times one (`1.years`).
"""
tabs """
annual_average_df = df.group_by_dynamic("Date", every="1y").agg(pl.col("Close").mean())
df_with_year = annual_average_df.with_columns(pl.col("Date").dt.year().alias("year"))
df_with_year
""":
nbCode:
let annualAverageDf = df.groupByDynamic("Date", every = "1y")
.agg(col.Close.mean)
let dfWithYear = annualAverageDf.withColumns(col("Date").dt.year.alias("year"))
nbShow dfWithYear
nbText: """
### Parameters for `group_by_dynamic`
A dynamic window is defined by a:
- **every**: indicates the interval of the window
- **period**: indicates the duration of the window
- **offset**: can be used to offset the start of the windows
The value for `every` sets how often the groups start. The time period values are flexible
- for example we could take:
- the average over 2 year intervals by replacing `1y` with `2y`
- the average over 18 month periods by replacing `1y` with `1y6mo`
We can also use the `period` parameter to set how long the time period for each group is.
For example, if we set the `every` parameter to be `1y` and the `period` parameter to be
`2y` then we would get groups at one year intervals where each groups spanned two years.
If the `period` parameter is not specified then it is set equal to the `every` parameter
so that if the `every` parameter is set to be `1y` then each group spans `1y` as well.
Because _**every**_ does not have to be equal to _**period**_, we can create many groups
in a very flexible way. They may overlap or leave boundaries between them.
Let's see how the windows for some parameter combinations would look. Let's start out
boring. π₯±
- every: 1 day -> `"1d"`
- period: 1 day -> `"1d"`
```text
this creates adjacent windows of the same size
|--|
|--|
|--|
```
- every: 1 day -> `"1d"`
- period: 2 days -> `"2d"`
```text
these windows have an overlap of 1 day
|----|
|----|
|----|
```
- every: 2 days -> `"2d"`
- period: 1 day -> `"1d"`
```text
this would leave gaps between the windows
data points that in these gaps will not be a member of any group
|--|
|--|
|--|
```
#### `truncate`
The `truncate` parameter is a Boolean variable that determines what datetime value is
associated with each group in the output. In the example above the first data point is on
23rd February 1981. If `truncate = True` (the default) then the date for the first year in
the annual average is 1st January 1981. However, if `truncate = False` then the date for
the first year in the annual average is the date of the first data point on 23rd February
1981. Note that `truncate` only affects what's shown in the `Date` column and does not
affect the window boundaries.
"""
nanuq """
Current Polars has no `truncate` parameter any more; its `label` parameter (`Label.Left`,
the default, `Right` or `DataPoint`) decides which date names a window, and Nanuq has it
too.
"""
nbText: """
### Using expressions in `group_by_dynamic`
We aren't restricted to using simple aggregations like `mean` in a group by operation - we
can use the full range of expressions available in Polars.
In the snippet below we create a `date range` with every **day** (`"1d"`) in 2021 and turn
this into a `DataFrame`.
Then in the `group_by_dynamic` we create dynamic windows that start every **month**
(`"1mo"`) and have a window length of `1` month. The values that match these dynamic
windows are then assigned to that group and can be aggregated with the powerful expression
API.
Below we show an example where we use **group_by_dynamic** to compute:
- the number of days until the end of the month
- the number of days in a month
"""
tabs """
df = (
pl.date_range(
start=date(2021, 1, 1),
end=date(2021, 12, 31),
interval="1d",
eager=True,
)
.alias("time")
.to_frame()
)
df.group_by_dynamic("time", every="1mo", period="1mo", closed="left").agg(
pl.col("time").cum_count().reverse().head(3).alias("day/eom"),
((pl.col("time") - pl.col("time").first()).last().dt.total_days() + 1).alias(
"days_in_month"
),
)
""":
nbCode:
let days = newDataFrame().select(
dateRange(start = date(2021, 1, 1), stop = date(2021, 12, 31), interval = "1d")
.alias("time"))
nbShow days.groupByDynamic("time", every = "1mo", period = "1mo",
closed = ClosedWindow.Left).agg(
col.time.cumCount.reverse.head(3).alias("day/eom"),
((col.time - col.time.first).last.dt.totalDays + 1).alias("days_in_month"))
nanuq """
`dateRange` is an expression, so Nanuq selects it from an empty frame (there is no
`eager=True`). The window's closed side is a value of the enum `ClosedWindow`.
"""
nbText: """
## Grouping by rolling windows
The rolling operation, `rolling`, is another entrance to the `group_by`/`agg` context. But
different from the `group_by_dynamic` where the windows are fixed by a parameter `every`
and `period`. In a `rolling`, the windows are not fixed at all! They are determined by the
values in the `index_column`.
So imagine having a time column with the values `{2021-01-06, 2021-01-10}` and a
`period="5d"` this would create the following windows:
```text
2021-01-01 2021-01-06
|----------|
2021-01-05 2021-01-10
|----------|
```
Because the windows of a rolling group by are always determined by the values in the
`DataFrame` column, the number of groups is always equal to the original `DataFrame`.
"""
nanuq """
The original has no code for `rolling`. In Nanuq it is `df.rolling("time", period = "5d")`
followed by `agg`, like `groupByDynamic`.
"""
nbText: """
## Combining group by operations
Rolling and dynamic group by operations can be combined with normal group by operations.
Below is an example with a dynamic group by.
"""
tabs """
df = pl.DataFrame(
{
"time": pl.datetime_range(
start=datetime(2021, 12, 16),
end=datetime(2021, 12, 16, 3),
interval="30m",
eager=True,
),
"groups": ["a", "a", "a", "b", "b", "a", "a"],
}
)
df
""":
nbCode:
let df2 = newDataFrame().select(
datetimeRange(start = dateTime(2021, mDec, 16, zone = utc()),
stop = dateTime(2021, mDec, 16, 3, zone = utc()),
interval = "30m").alias("time")
).withColumns(series("groups", ["a", "a", "a", "b", "b", "a", "a"]))
nbShow df2
nanuq """
`group_by` within the windows is `groupBy`, a list of expressions or column names.
"""
tabs """
df.group_by_dynamic(
"time",
every="1h",
closed="both",
group_by="groups",
include_boundaries=True,
).agg(pl.len())
""":
nbCode:
let byHour = df2.groupByDynamic(
"time",
every = "1h",
closed = ClosedWindow.Both,
groupBy = [col.groups],
includeBoundaries = true,
).agg(len())
nbShow byHour
bookSave()
On GitHub: book/user-guide/time_series_grouping.nim