Group By
DimensionalData.jl provides a groupby function for dimensional grouping. This guide covers:
simple grouping with a function
grouping with
Binsgrouping with another existing
AbstractDimArrayorDimension
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:
using DimensionalData
using Dates
using Statistics
const DD = DimensionalDataNow create a demo DateTime range
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
hour.(tempo)Tuple groupings
yearmonth.(tempo)Grouping and reducing
Let's define an array with a time dimension of the times used above:
A = rand(X(1:0.01:2), Ti(tempo))Group by month, using the month function:
groups = groupby(A, Ti=>month)We can take the mean of each group by broadcasting over them:
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.
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:
groupby(A, dims(A, Ti))TODO: Apply custom function (i.e. normalization) to grouped output.