pydata / pydata/xarray

Confusing error message when reducing over non-existent dimension

Open
#10,875 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

What happened?

When reducing a DataArray after using groupby, and reducing over a dimension that does not exist, the given error is

ValueError: cannot reduce over dimensions ['<missing dimension>']. expected either '...' to reduce over all dimensions or one or more of (<existing dimension). Alternatively, install the `flox` package.

Doing the same but without using groupby gives a much more clear error

ValueError: '<missing dimension>' not found in array dimensions (<existing dimensions>)
What did you expect to happen?

I'd expect both examples to give the error

ValueError: '<missing dimension>' not found in array dimensions (<existing dimensions>)

which is a much more clear indication of what caused the error

Minimal Complete Verifiable Example
import numpy as np
import xarray as xr
ds = xr.DataArray(np.reshape(range(27), (3, 3, 3)),
                  coords=dict(lon=range(3),
                              lat=range(3),
                              time=xr.date_range('2025-10-01 00:00', '2025-10-01 02:00', freq='h')))
ds.std(dim='lon')  # OK
ds.groupby('time').std(dim='lon')  # OK
ds.std(dim='longitude')  # ValueError: 'longitude' not found in array dimensions ('lon', 'lat', 'time')
ds.groupby('time').std(dim='longitude')  # ValueError: cannot reduce over dimensions ['longitude']. expected either '...' to reduce over all dimensions or one or more of ('lon', 'lat', 'time'). Alternatively, install the `flox` package.
Steps to reproduce

No response

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

Anything else we need to know?

No response

Environment
INSTALLED VERSIONS ------------------ commit: None python: 3.14.0 | packaged by conda-forge | (main, Oct 22 2025, 23:18:03) [MSC v.1944 64 bit (AMD64)] python-bits: 64 OS: Windows OS-release: 11 machine: AMD64 processor: Intel64 Family 6 Model 189 Stepping 1, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: ('English_Netherlands', '1252') libhdf5: None libnetcdf: None

xarray: 2025.10.1
pandas: 2.3.3
numpy: 2.3.4
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 25.2
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None

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 supplied xarray DataArray example and compare the plain std(dim='longitude') error with the groupby('time').std(dim='longitude') error. Trace the groupby reduction path that produces the misleading message; the work is done when the grouped case reports the missing dimension and existing dimensions as shown in the expected error.

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.