Skip to content

Bins fails when the grouping function returns non-numeric types #1149

@shy86

Description

@shy86

When using groupby with Bins in YAXArrays, the grouping works correctly only if the mapping function returns simple numeric types. If the function returns other types—such as strings produced by format—the bins produce empty groups or throw an error.

For example, I want to groupby year and month:

using Dates
using YAXArrays
using YAXArrays: YAXArrays as YAX

axlist = (
    YAX.time(Date("1961-01-01"):Day(1):Date("1961-04-30")),
    YAX.lon(1:10),
    YAX.lat(1:15),
)

data = rand(120, 10, 15)

a = YAXArray(axlist, data)

# Method 1: works
groupby(a, YAX.time => Bins(x -> year(x) * 100 + month(x), [[196101, 196102], [196103, 196104]]))

# Method 2: failed
groupby(a, YAX.time => Bins(x -> format(x, "yyyymm"), [["196101", "196102"], ["196103", "196104"]]))

# Method 3: failed
groupby(a, YAX.time => Bins(yearmonth, [[(1961, 1), (1961, 2)], [(1961, 3), (1961, 4)]]))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions