Window functions

Window functions are expressions with superpowers. They allow you to perform aggregations on groups within the context select. Let's get a feel for what that means.

First, we load a Pokémon dataset:

import nanuq

type
  PokemonType = enum
    Grass, Water, Fire, Normal, Ground, Electric, Psychic, Fighting, Bug, Steel,
    Flying, Dragon, Dark, Ghost, Poison, Rock, Ice, Fairy

let typeEnum = dataType(PokemonType)
# then let's load some csv data with information about pokemon
let pokemon = readCsv(
  "data/polars/pokemon.csv",
).cast({"Type 1": typeEnum, "Type 2": typeEnum})
nbShow pokemon.head()
shape: (5, 13)
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
i64strenumenumi64i64i64i64i64i64i64i64bool
1"Bulbasaur""Grass""Poison"3184549496565451false
2"Ivysaur""Grass""Poison"4056062638080601false
3"Venusaur""Grass""Poison"525808283100100801false
3"VenusaurMega Venusaur""Grass""Poison"62580100123122120801false
4"Charmander""Fire"null3093952436050651false

Operations per group

Window functions are ideal when we want to perform an operation within a group. For instance, suppose we want to rank our Pokémon by the column “Speed”. However, instead of a global ranking, we want to rank the speed within each group defined by the column “Type 1”. We write the expression to rank the data by the column “Speed” and then we add the function over to specify that this should happen over the unique values of the column “Type 1”:

var result = pokemon.select(
  col("Name", "Type 1"),
  col.Speed.rank(Dense, descending = true).over("Type 1").alias("Speed rank"))

nbShow result
shape: (163, 3)
NameType 1Speed rank
strenumu32
"Bulbasaur""Grass"6
"Ivysaur""Grass"3
"Venusaur""Grass"1
"VenusaurMega Venusaur""Grass"1
"Charmander""Fire"7
………
"Moltres""Fire"5
"Dratini""Dragon"3
"Dragonair""Dragon"2
"Dragonite""Dragon"1
"Mewtwo""Psychic"2

To help visualise this operation, you may imagine that Polars selects the subsets of the data that share the same value for the column “Type 1” and then computes the ranking expression only for those values. Then, the results for that specific group are projected back to the original rows and Polars does this for all of the existing groups. In the Polars user guide, a diagram highlights the ranking computation for the Pokémon with “Type 1” equal to “Grass”.

Note how the row for the Pokémon “Golbat” has a “Speed” value of 90, which is greater than the value 80 of the Pokémon “Venusaur”, and yet the latter was ranked 1 because “Golbat” and “Venusar” do not share the same value for the column “Type 1”.

The function over accepts an arbitrary number of expressions to specify the groups over which to perform the computations. We can repeat the ranking above, but over the combination of the columns “Type 1” and “Type 2” for a more fine-grained ranking:

result = pokemon.select(
  col("Name", "Type 1", "Type 2"),
  col.Speed
    .rank(Dense, descending = true)
    .over(["Type 1", "Type 2"])
    .alias("Speed rank"))

nbShow result
shape: (163, 4)
NameType 1Type 2Speed rank
strenumenumu32
"Bulbasaur""Grass""Poison"6
"Ivysaur""Grass""Poison"3
"Venusaur""Grass""Poison"1
"VenusaurMega Venusaur""Grass""Poison"1
"Charmander""Fire"null7
…………
"Moltres""Fire""Flying"2
"Dratini""Dragon"null2
"Dragonair""Dragon"null1
"Dragonite""Dragon""Flying"1
"Mewtwo""Psychic"null2

In general, the results you get with the function over can also be achieved with an aggregation followed by a call to the function explode, although the rows would be in a different order:

nbShow pokemon.groupBy("Type 1")
  .agg(
    col.Name,
    col.Speed.rank(Dense, descending = true).alias("Speed rank"))
  .select(col.Name, col("Type 1"), col("Speed rank"))
  .explode("Name", "Speed rank")
shape: (163, 3)
NameType 1Speed rank
strenumu32
"Bulbasaur""Grass"6
"Ivysaur""Grass"3
"Venusaur""Grass"1
"VenusaurMega Venusaur""Grass"1
"Oddish""Grass"8
………
"Jynx""Ice"1
"Articuno""Ice"2
"Dratini""Dragon"3
"Dragonair""Dragon"2
"Dragonite""Dragon"1

This shows that, usually, group_by and over produce results of different shapes:

  • group_by usually produces a resulting dataframe with as many rows as groups used for aggregating; and
  • over usually produces a dataframe with the same number of rows as the original.

The function over does not always produce results with the same number of rows as the original dataframe, and that is what we explore next.

Mapping results to dataframe rows

The function over accepts a parameter mapping_strategy that determines how the results of the expression over the group are mapped back to the rows of the dataframe.

group_to_rows

The default behaviour is "group_to_rows": the result of the expression over the group should be the same length as the group and the results are mapped back to the rows of that group.

If the order of the rows is not relevant, the option "explode" is more performant. Instead of mapping the resulting values to the original rows, Polars creates a new dataframe where values from the same group are next to each other. To help understand the distinction, consider the following dataframe:

let athletes = newDataFrame:
  athlete: ["A", "B", "C", "D", "E", "F"]
  country: ["PT", "NL", "NL", "PT", "PT", "NL"]
  rank: [6, 1, 5, 4, 2, 3]
nbShow athletes
shape: (6, 3)
athletecountryrank
strstri64
"A""PT"6
"B""NL"1
"C""NL"5
"D""PT"4
"E""PT"2
"F""NL"3

We can sort the athletes by rank within their own countries. If we do so, the Dutch athletes were in the second, third, and sixth, rows, and they will remain there. What will change is the order of the names of the athletes, which goes from “B”, “C”, and “F”, to “B”, “F”, and “C”:

let sortedByRank = athletes.select(
  col(["athlete", "rank"]).sortBy(col.rank).over(col.country),
  col.country)
nbShow sortedByRank
shape: (6, 3)
athleterankcountry
stri64str
"E"2"PT"
"B"1"NL"
"F"3"NL"
"D"4"PT"
"A"6"PT"
"C"5"NL"

explode

If we set the parameter mapping_strategy to "explode", then athletes of the same country are grouped together, but the final order of the rows – with respect to the countries – will not be the same.

Because Polars does not need to keep track of the positions of the rows of each group, using "explode" is typically faster than "group_to_rows". However, using "explode" also requires more care because it implies reordering the other columns that we wish to keep. The code that produces this result follows

result = athletes.select(
    pl.all()
    .sort_by(pl.col("rank"))
    .over(pl.col("country"), mapping_strategy="explode"),
)

result
shape: (6, 3)
athletecountryrank
strstri64
"E""PT"2
"D""PT"4
"A""PT"6
"B""NL"1
"F""NL"3
"C""NL"5

join

Another possible value for the parameter mapping_strategy is "join", which aggregates the resulting values in a list and repeats the list over all rows of the same group:

result = athletes.with_columns(
    pl.col("rank").sort().over(pl.col("country"), mapping_strategy="join"),
)

result
shape: (6, 3)
athletecountryrank
strstrlist[i64]
"A""PT"[2, 4, 6]
"B""NL"[1, 3, 5]
"C""NL"[1, 3, 5]
"D""PT"[2, 4, 6]
"E""PT"[2, 4, 6]
"F""NL"[1, 3, 5]

Windowed aggregation expressions

In case the expression applied to the values of a group produces a scalar value, the scalar is broadcast across the rows of the group:

result = pokemon.select(
  col("Name", "Type 1", "Speed"),
  col.Speed.mean.over([col("Type 1")]).alias("Mean speed in group"))

nbShow result
shape: (163, 4)
NameType 1SpeedMean speed in group
strenumi64f64
"Bulbasaur""Grass"4554.230769
"Ivysaur""Grass"6054.230769
"Venusaur""Grass"8054.230769
"VenusaurMega Venusaur""Grass"8054.230769
"Charmander""Fire"6586.285714
…………
"Moltres""Fire"9086.285714
"Dratini""Dragon"5066.666667
"Dragonair""Dragon"7066.666667
"Dragonite""Dragon"8066.666667
"Mewtwo""Psychic"13099.25

More examples

For more exercises, below are some window functions for us to compute:

  • sort all Pokémon by type;
  • select the first 3 Pokémon per type as "Type 1";
  • sort the Pokémon within a type by speed in descending order and select the first 3 as "fastest/group";
  • sort the Pokémon within a type by attack in descending order and select the first 3 as "strongest/group"; and
  • sort the Pokémon within a type by name and select the first 3 as "sorted_by_alphabet".
result = pokemon.sort("Type 1").select(
    pl.col("Type 1").head(3).over("Type 1", mapping_strategy="explode"),
    pl.col("Name")
    .sort_by(pl.col("Speed"), descending=True)
    .head(3)
    .over("Type 1", mapping_strategy="explode")
    .alias("fastest/group"),
    pl.col("Name")
    .sort_by(pl.col("Attack"), descending=True)
    .head(3)
    .over("Type 1", mapping_strategy="explode")
    .alias("strongest/group"),
    pl.col("Name")
    .sort()
    .head(3)
    .over("Type 1", mapping_strategy="explode")
    .alias("sorted_by_alphabet"),
)
result
shape: (43, 4)
Type 1fastest/groupstrongest/groupsorted_by_alphabet
enumstrstrstr
"Grass""Venusaur""Victreebel""Bellsprout"
"Grass""VenusaurMega Venusaur""VenusaurMega Venusaur""Bulbasaur"
"Grass""Victreebel""Exeggutor""Exeggcute"
"Water""Starmie""GyaradosMega Gyarados""Blastoise"
"Water""Tentacruel""Kingler""BlastoiseMega Blastoise"
…………
"Rock""Kabutops""Kabutops""Geodude"
"Ice""Jynx""Articuno""Articuno"
"Ice""Articuno""Jynx""Jynx"
"Fairy""Clefable""Clefable""Clefable"
"Fairy""Clefairy""Clefairy""Clefairy"
import nanuqbook
bookInit()

nbText: "# Window functions"

attribution userGuide("Window functions", "expressions/window-functions",
  "The data is read from `data/polars/pokemon.csv`. The examples with the " &
  "`mapping_strategy` parameter run in Python only, since Nanuq does not have it yet. " &
  "The diagrams are left out.")

nbText: """
Window functions are expressions with superpowers. They allow you to perform aggregations
on groups within the context `select`. Let's get a feel for what that means.

First, we load a Pokémon dataset:
"""

tabs """
import polars as pl

types = (
    "Grass Water Fire Normal Ground Electric Psychic Fighting Bug Steel "
    "Flying Dragon Dark Ghost Poison Rock Ice Fairy".split()
)
type_enum = pl.Enum(types)
# then let's load some csv data with information about pokemon
pokemon = pl.read_csv(
    "data/polars/pokemon.csv",
).cast({"Type 1": type_enum, "Type 2": type_enum})
pokemon.head()
""":
  nbCode:
    import nanuq

    type
      PokemonType = enum
        Grass, Water, Fire, Normal, Ground, Electric, Psychic, Fighting, Bug, Steel,
        Flying, Dragon, Dark, Ghost, Poison, Rock, Ice, Fairy

    let typeEnum = dataType(PokemonType)
    # then let's load some csv data with information about pokemon
    let pokemon = readCsv(
      "data/polars/pokemon.csv",
    ).cast({"Type 1": typeEnum, "Type 2": typeEnum})
    nbShow pokemon.head()

nanuq """
The Polars `Enum` is a Nim enum here, `PokemonType`, and `dataType` turns it into the
data type to cast to. `Enum(types)`, with the names as strings, would work too.
"""

nbText: """
## Operations per group

Window functions are ideal when we want to perform an operation within a group. For
instance, suppose we want to rank our Pokémon by the column “Speed”. However, instead of a
global ranking, we want to rank the speed within each group defined by the column “Type 1”.
We write the expression to rank the data by the column “Speed” and then we add the function
`over` to specify that this should happen over the unique values of the column “Type 1”:
"""

tabs """
result = pokemon.select(
    pl.col("Name", "Type 1"),
    pl.col("Speed").rank("dense", descending=True).over("Type 1").alias("Speed rank"),
)

result
""":
  nbCode:
    var result = pokemon.select(
      col("Name", "Type 1"),
      col.Speed.rank(Dense, descending = true).over("Type 1").alias("Speed rank"))

    nbShow result

nbText: """
To help visualise this operation, you may imagine that Polars selects the subsets of the
data that share the same value for the column “Type 1” and then computes the ranking
expression only for those values. Then, the results for that specific group are projected
back to the original rows and Polars does this for all of the existing groups. In the
Polars user guide, a diagram highlights the ranking computation for the Pokémon with “Type
1” equal to “Grass”.

Note how the row for the Pokémon “Golbat” has a “Speed” value of `90`, which is greater
than the value `80` of the Pokémon “Venusaur”, and yet the latter was ranked 1 because
“Golbat” and “Venusar” do not share the same value for the column “Type 1”.

The function `over` accepts an arbitrary number of expressions to specify the groups over
which to perform the computations. We can repeat the ranking above, but over the
combination of the columns “Type 1” and “Type 2” for a more fine-grained ranking:
"""

tabs """
result = pokemon.select(
    pl.col("Name", "Type 1", "Type 2"),
    pl.col("Speed")
    .rank("dense", descending=True)
    .over("Type 1", "Type 2")
    .alias("Speed rank"),
)

result
""":
  nbCode:
    result = pokemon.select(
      col("Name", "Type 1", "Type 2"),
      col.Speed
        .rank(Dense, descending = true)
        .over(["Type 1", "Type 2"])
        .alias("Speed rank"))

    nbShow result

nanuq """
Nim's `over` takes several columns in an array, `over(["Type 1", "Type 2"])`, or
expressions, `over([col("Type 1"), col("Type 2")])`.
"""

nbText: """
In general, the results you get with the function `over` can also be achieved with
[an aggregation](https://docs.pola.rs/user-guide/expressions/aggregation/) followed by a
call to the function `explode`, although the rows would be in a different order:
"""

nanuq """
The Python side adds `maintain_order=True`, so that its groups come in the same order as
Nanuq's, which always keeps them in the order they are first seen.
"""

tabs """
result = (
    pokemon.group_by("Type 1", maintain_order=True)
    .agg(
        pl.col("Name"),
        pl.col("Speed").rank("dense", descending=True).alias("Speed rank"),
    )
    .select(pl.col("Name"), pl.col("Type 1"), pl.col("Speed rank"))
    .explode("Name", "Speed rank")
)

result
""":
  nbCode:
    nbShow pokemon.groupBy("Type 1")
      .agg(
        col.Name,
        col.Speed.rank(Dense, descending = true).alias("Speed rank"))
      .select(col.Name, col("Type 1"), col("Speed rank"))
      .explode("Name", "Speed rank")

nbText: """
This shows that, usually, `group_by` and `over` produce results of different shapes:

- `group_by` usually produces a resulting dataframe with as many rows as groups used for
  aggregating; and
- `over` usually produces a dataframe with the same number of rows as the original.

The function `over` does not always produce results with the same number of rows as the
original dataframe, and that is what we explore next.

## Mapping results to dataframe rows

The function `over` accepts a parameter `mapping_strategy` that determines how the results
of the expression over the group are mapped back to the rows of the dataframe.

### `group_to_rows`

The default behaviour is `"group_to_rows"`: the result of the expression over the group
should be the same length as the group and the results are mapped back to the rows of that
group.

If the order of the rows is not relevant, the option `"explode"` is more performant.
Instead of mapping the resulting values to the original rows, Polars creates a new
dataframe where values from the same group are next to each other. To help understand the
distinction, consider the following dataframe:
"""

tabs """
athletes = pl.DataFrame(
    {
        "athlete": list("ABCDEF"),
        "country": ["PT", "NL", "NL", "PT", "PT", "NL"],
        "rank": [6, 1, 5, 4, 2, 3],
    }
)
athletes
""":
  nbCode:
    let athletes = newDataFrame:
      athlete: ["A", "B", "C", "D", "E", "F"]
      country: ["PT", "NL", "NL", "PT", "PT", "NL"]
      rank: [6, 1, 5, 4, 2, 3]
    nbShow athletes

nanuq """
Nanuq's `over` has no `mapping_strategy` yet, so only the default, `group_to_rows`, has a
Nim version in this section.
"""

nbText: """
We can sort the athletes by rank within their own countries. If we do so, the Dutch
athletes were in the second, third, and sixth, rows, and they will remain there. What will
change is the order of the names of the athletes, which goes from “B”, “C”, and “F”, to
“B”, “F”, and “C”:
"""

tabs """
result = athletes.select(
    pl.col("athlete", "rank").sort_by(pl.col("rank")).over(pl.col("country")),
    pl.col("country"),
)

result
""":
  nbCode:
    let sortedByRank = athletes.select(
      col(["athlete", "rank"]).sortBy(col.rank).over(col.country),
      col.country)
    nbShow sortedByRank

nbText: """
### `explode`

If we set the parameter `mapping_strategy` to `"explode"`, then athletes of the same
country are grouped together, but the final order of the rows – with respect to the
countries – will not be the same.

Because Polars does not need to keep track of the positions of the rows of each group,
using `"explode"` is typically faster than `"group_to_rows"`. However, using `"explode"`
also requires more care because it implies reordering the other columns that we wish to
keep. The code that produces this result follows
"""

python """
result = athletes.select(
    pl.all()
    .sort_by(pl.col("rank"))
    .over(pl.col("country"), mapping_strategy="explode"),
)

result
"""

nbText: """
### `join`

Another possible value for the parameter `mapping_strategy` is `"join"`, which aggregates
the resulting values in a list and repeats the list over all rows of the same group:
"""

python """
result = athletes.with_columns(
    pl.col("rank").sort().over(pl.col("country"), mapping_strategy="join"),
)

result
"""

nbText: """
## Windowed aggregation expressions

In case the expression applied to the values of a group produces a scalar value, the
scalar is broadcast across the rows of the group:
"""

tabs """
result = pokemon.select(
    pl.col("Name", "Type 1", "Speed"),
    pl.col("Speed").mean().over(pl.col("Type 1")).alias("Mean speed in group"),
)

result
""":
  nbCode:
    result = pokemon.select(
      col("Name", "Type 1", "Speed"),
      col.Speed.mean.over([col("Type 1")]).alias("Mean speed in group"))

    nbShow result

nbText: """
## More examples

For more exercises, below are some window functions for us to compute:

- sort all Pokémon by type;
- select the first `3` Pokémon per type as `"Type 1"`;
- sort the Pokémon within a type by speed in descending order and select the first `3` as
  `"fastest/group"`;
- sort the Pokémon within a type by attack in descending order and select the first `3` as
  `"strongest/group"`; and
- sort the Pokémon within a type by name and select the first `3` as `"sorted_by_alphabet"`.
"""

python """
result = pokemon.sort("Type 1").select(
    pl.col("Type 1").head(3).over("Type 1", mapping_strategy="explode"),
    pl.col("Name")
    .sort_by(pl.col("Speed"), descending=True)
    .head(3)
    .over("Type 1", mapping_strategy="explode")
    .alias("fastest/group"),
    pl.col("Name")
    .sort_by(pl.col("Attack"), descending=True)
    .head(3)
    .over("Type 1", mapping_strategy="explode")
    .alias("strongest/group"),
    pl.col("Name")
    .sort()
    .head(3)
    .over("Type 1", mapping_strategy="explode")
    .alias("sorted_by_alphabet"),
)
result
"""

nanuq """
Python only as well: Nanuq has `sortBy` and `head` on expressions, but not
`mapping_strategy`.
"""

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