Confusing error message when reducing over non-existent dimension
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
- [x] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- [x] Complete example — the example is self-contained, including all data and the text of any traceback.
- [x] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- [x] New issue — a search of GitHub Issues suggests this is not a duplicate.
- [x] 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
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
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
The error is here: https://github.com/pydata/xarray/blob/e49cfc4f2009eb660411128eaf05fa3e714a0c83/xarray/core/groupby.py#L78
and is confusing because we check against a possibly stacked object https://github.com/pydata/xarray/blob/e49cfc4f2009eb660411128eaf05fa3e714a0c83/xarray/core/groupby.py#L679-L681
It may be simplest to reword the error message