Consolidate and standardize grouping API #1212
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR standardizes the grouping API and allows user-defined classes to support GroupBy with other arrays by defining the API methods.
Integral pdarrays, Strings, and Categoricals now suppport the grouping API below. For a user-defined class to be groupable, it must inherit from
ak.pdarray
and define or overload the grouping API:._get_grouping_keys()
method that returns a list of pdarrays that can be (co)argsorted..group()
method that directly returns the permutation that groups the arrayIf the input is a single array with a
.group()
method defined, method 2 will be used to find the permuation; otherwise, method 1 will be used. To find the segment offsets and unique keys, the keys from._get_grouping_keys()
are always used, so that method must always be defined.In the future, I plan to consolidate grouping even further under the
ak.unique()
function, so that it returns the permutation, segments, and unique keys all from one place. However, this requires more extensive changes to the server, whereas this PR helps us move incrementally in that direction while meeting an immediate need for grouping on user-defined classes with non-standard grouping keys.