pydata / pydata/xarray

groupby multiple variables should include observed groups only

Open
#11,178 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

plan to close topic-groupby usage question
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

What happened?

Grouping more more than one variable includes combinations with no observations

What did you expect to happen?

Empty groups are not included

Minimal Complete Verifiable Example
# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "xarray[complete]@git+https://github.com/pydata/xarray.git@main",
# ]
# ///
#
# This script automatically imports the development branch of xarray to check for issues.
# Please delete this header if you have _not_ tested this script with `uv run`!

import numpy as np
import xarray as xr
xr.show_versions()

coords = {'y': [0, 1, 2, 3], 'y1': ('y', [1, 1, 2, 2]), 'y2': ('y', [1, 1, 2, 2])}
xdata = xr.DataArray(data=np.ones((4, 2)), coords=coords, dims=['y', 'x'])

xdata.groupby(['y1', 'y2']).mean()
MVCE confirmation
  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output

<xarray.DataArray (y1: 2, y2: 2, x: 2)> Size: 64B
array([[[ 1.,  1.],
        [nan, nan]],
       [[nan, nan],
        [ 1.,  1.]]])
Coordinates:
  * y1       (y1) int64 16B 1 2
  * y2       (y2) int64 16B 1 2
Dimensions without coordinates: x

Environment

INSTALLED VERSIONS

commit: None
python: 3.12.12 | packaged by conda-forge | (main, Jan 27 2026, 00:01:15) [Clang 19.1.7 ]
python-bits: 64
OS: Darwin
OS-release: 25.2.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('C', 'UTF-8')
libhdf5: 1.14.6
libnetcdf: None
xarray: 2026.2.0
pandas: 2.3.3
numpy: 1.26.4
scipy: 1.16.3
netCDF4: None
pydap: None
h5netcdf: None
h5py: 3.15.1
zarr: 2.18.7
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: 2025.11.0
distributed: 2025.11.0
matplotlib: 3.10.8
cartopy: None
seaborn: 0.13.2
numbagg: None
fsspec: 2026.2.0
cupy: None
pint: 0.25.2
sparse: 0.17.0
flox: 0.11.1
numpy_groupies: 0.11.3
setuptools: 82.0.0
pip: 26.0.1
conda: None
pytest: 9.0.2
mypy: None
IPython: 9.10.0
sphinx: 9.1.0

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 with the provided Python MVCE and inspect xarray's multi-variable groupby implementation and its tests, using the reported output as the baseline. Verify that grouping by y1 and y2 returns only observed combinations (1,1) and (2,2), without empty combinations such as (1,2) or (2,1).

Written by the indexing model from the issue text.

Assessment

Tech stack
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.