Skip to content

Group By ​

DimensionalData.jl provides a groupby function for dimensional grouping. This guide covers:

  • simple grouping with a function

  • grouping with Bins

  • grouping with another existing AbstractDimArray or Dimension

Grouping functions ​

Let's look at the kind of functions that can be used to group DateTime. Other types will follow the same principles, but are usually simpler.

First, load some packages:

julia
using DimensionalData
using Dates
using Statistics
const DD = DimensionalData

Now create a demo DateTime range

@ansi
tempo = range(DateTime(2000), step=Hour(1), length=365*24*2)

Let's see how some common functions work.

The hour function will transform values to the hour of the day - the integers 0:23

@ansi
hour.(tempo)

Tuple groupings

@ansi
yearmonth.(tempo)

Grouping and reducing ​

Let's define an array with a time dimension of the times used above:

@ansi
A = rand(X(1:0.01:2), Ti(tempo))

Group by month, using the month function:

@ansi
groups = groupby(A, Ti=>month)

We can take the mean of each group by broadcasting over them:

@ansi
mean.(groups)

Binning ​

Sometimes we want to further aggregate our groups after running a function, or just bin the raw data directly. We can use the Bins wrapper to do this.

For quick analysis, we can break our groups into N bins.

@ansi
groupby(A, Ti=>Bins(month, 4))

Doing this requires slightly padding the bin edges, so the lookup of the output is less than ideal.

Select by Dimension ​

We can also select by Dimensions and any objects with dims methods.

Trivially, grouping by an object's own dimension is similar to eachslice:

@ansi
groupby(A, dims(A, Ti))

TODO: Apply custom function (i.e. normalization) to grouped output.