Note on eachindex behavior with GroupedDataFrame

eachindex used on a GroupedDataFrame, returns a GroupKey, such as GroupKey: (site = 'b',).

When using the indices to create figure layouts in Makie or other plotting packages, (Axis(fig[i, 1]), one should explicitly ask for IndexLinear from eachindex.

demo code
using DataFrames

"""
  gdfindices()

Demonstrate behavior of `eachindex` with GroupedDataFrame.  
"""
function gdfindices()
    df = DataFrame(site = rand('a':'e', 1000), yield = rand(1:100, 1000))
    grpdf = groupby(df, :site)       
    println(" Keys:")
    show(stdout, "text/plain", keys(grpdf))

    naiveindx =  eachindex(grpdf)
    println("\n\n eachindex(grpdf):")
    show(stdout, "text/plain", naiveindx)

    indx = eachindex(IndexLinear(), grpdf)
    println("\n\n eachindex, IndexLinear:")
    show(stdout, "text/plain", indx)
end

julia> gdfindices()
 Keys:
5-element DataFrames.GroupKeys{DataFrames.GroupedDataFrame{DataFrames.DataFrame}}:
 GroupKey: (site = 'b',)
 GroupKey: (site = 'c',)
 GroupKey: (site = 'd',)
 GroupKey: (site = 'a',)
 GroupKey: (site = 'e',)

 eachindex(grpdf):
5-element DataFrames.GroupKeys{DataFrames.GroupedDataFrame{DataFrames.DataFrame}}:
 GroupKey: (site = 'b',)
 GroupKey: (site = 'c',)
 GroupKey: (site = 'd',)
 GroupKey: (site = 'a',)
 GroupKey: (site = 'e',)

 eachindex, IndexLinear:
Base.OneTo(5)
1 Like

In plotting I tend to iterate over enumerate(pairs(gdf)). I use the running index to get the axis location with fldmod1(i, ncols) and use the keys to set titles or colors etc per group.

1 Like