pydata / pydata/xarray

Reimplement GroupBy.argmax

Open
#4,476 6 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

regression topic-groupby
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Please implement

Is your feature request related to a problem? Please describe.
Observed:

da.groupby("g").argmax(dim="t")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-84-15c199b0f7d4> in <module>
----> 1 da.groupby("g").argmax(dim="t")

AttributeError: 'DataArrayGroupBy' object has no attribute 'argmax'

Describe the solution you'd like
Expected: Vector of length len(unique(g)) containing the indices of da["t"] where the value was maximum.

Workaround:

da.groupby("g").apply(lambda c: c.argmax(dim="t"))
<xarray.DataArray 'da' (st: 11, g: 1)>
array([[ 7],
       [ 0],
       [14],
       [14],
       [ 0],
       [ 0],
       [ 7],
       [ 0],
       [14],
       [ 0],
       [ 7]])
Coordinates:
  * st      (st) object 'a' ... 'z'
  * g       (g) object 'E'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the DataArrayGroupBy entry point implicated by the missing argmax method and compare it with existing groupby reduction methods. Use the reported da.groupby("g").argmax(dim="t") case as the starting check; done means it returns one maximum-index value per unique g, matching the expected result and workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.