pydata / pydata/xarray

missing empty group when iterate over groupby_bins

Open
#4,011 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When I try to iterate over the object grouped returned by groupby_bins, I found that the empty group is missing silently. Here is a simple case:

array = xr.DataArray(np.arange(4), dims='dim_0')
# one of these bins will be empty
bins = [0,4,5]
grouped = array.groupby_bins('dim_0', bins)

for i, group in enumerate(grouped):
    print(str(i)+'  '+group)

When a bin contains no samples (bin of (4, 5]), the empty group will be dropped. Then how to iterate over the full bins even when some bins contain nothing? I've read this related issue #1019.
But my case here need the correct order in grouped and empty groups need to be iterated over.

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

Run the provided Python reproduction and inspect the groupby_bins iteration entry point in xarray. Confirm how bins are represented when one has no samples; done means iteration preserves bin order and yields the empty bin rather than silently dropping it.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.