Skip to content

Could we defer to flox for GroupBy.first? #9647

@max-sixty

Description

@max-sixty

Is your feature request related to a problem?

I was wondering why a groupby("foo").first() call was going so slowly — I think we run a python loop for this, rather than calling into flox:

xarray/xarray/core/groupby.py

Lines 1218 to 1231 in b9780e7

def _first_or_last(self, op, skipna, keep_attrs):
if all(
isinstance(maybe_slice, slice)
and (maybe_slice.stop == maybe_slice.start + 1)
for maybe_slice in self.encoded.group_indices
):
# NB. this is currently only used for reductions along an existing
# dimension
return self._obj
if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=True)
return self.reduce(
op, dim=[self._group_dim], skipna=skipna, keep_attrs=keep_attrs
)

Describe the solution you'd like

Could we call into flox? Numbagg has the routines...

Describe alternatives you've considered

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions