You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've edited this a bit since I've opened it since I've discovered a bit of a can of worms. I'm trying to figure out if this is a duplicate of other issues now - it wasn't when I started.
tldr; ds.rolling({'time' : 12}).mean(keepdims=False|True) makes no difference and doesn't raise or even warn - plus some other related oddities.
To my mind, it should probably at least warn?
Bit of a corner case, found it whilst working on expression rewriting
What did you expect to happen?
At a minimum, probably raise for keepdims=False and warn for keepdims=True, or in an ideal world raise for both with a TypeError?
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`!importnumpyasnpimportpandasaspdimportxarrayasxrfromxarray.testingimportassert_equalxr.show_versions()
# Create a toy dataset over timetime_range=pd.date_range("2000-01-01", "2019-12-31", freq="MS")
tas=np.random.rand(
len(time_range),
)
# Create an xarray Datasetds=xr.Dataset(
{
"tas": (
["time",],
tas,
),
},
coords={
"time": time_range,
},
)
print("This pops out a warning noting that dim kwarg has no effect")
ds.rolling({"time": 12}).mean(dim="time")
print("This doesn't pop out any warning, just silently ignoring it")
_keepdims=ds.rolling({"time": 12}).mean(keepdims=False)
_no_keepdims=ds.rolling({"time": 12}).mean(keepdims=True)
print("Using `xr.testing.assert_equal` to check that the two results are equal")
assert_equal(_keepdims, _no_keepdims)
print("----------")
print(_keepdims)
outputs
This pops out a warning noting that dim kwarg has no effect
/Users/u1166368/.cache/uv/environments-v2/uv-repro-cae15bde6b41ecb3/lib/python3.12/site-packages/xarray/computation/rolling.py:919: FutureWarning: Reductions are applied along the rolling dimension(s) '['time']'. Passing the 'dim' kwarg to reduction operations has no effect.
return self._dataset_implementation(
This doesn't pop out any warning, jut silently ignoring it
Using `xr.testing.assert_equal` to check that the two results are equal
----------
<xarray.Dataset> Size: 4kB
Dimensions: (time: 240)
Coordinates:
* time (time) datetime64[us] 2kB 2000-01-01 2000-02-01 ... 2019-12-01
Data variables:
tas (time) float64 2kB nan nan nan nan ... 0.4684 0.4808 0.4134 0.4793
Steps to reproduce
Run the script above
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.
(This is the stuff that grew out of #11519, which I thought was originally a docs mismatch and now looks more like a related family of bugs - works on all reductions on groupbys)
For brevity, I'm going to keep the code snippets shorter - but all of these are in that same environment as above. We'll use a slightly more realistic dataset:
This seems at odds with what happens with DatasetRolling.mean() ignoring the keepdims Flag (which feels like a very benign bug to me - it's kinda meaningless to set keepdims to False with it), but I think it's also meaningfully an actual bug? Definitely a bit of an edge case - but the results don't really make sense as they stand. I think fix for this second issue with DatasetGroupBy would be to either:
Explictly disallow keepdims on groupby()...
Put the coordinate back in?
I can see cases for either - both make sense to me. For consistency with the rolling version, I think explicitly disallowing it for both would be the way to go? It doesn't make a great deal of sense for either & does weird things.
What happened?
Note
I've edited this a bit since I've opened it since I've discovered a bit of a can of worms. I'm trying to figure out if this is a duplicate of other issues now - it wasn't when I started.
tldr;
ds.rolling({'time' : 12}).mean(keepdims=False|True)makes no difference and doesn't raise or even warn - plus some other related oddities.To my mind, it should probably at least warn?
Bit of a corner case, found it whilst working on expression rewriting
What did you expect to happen?
At a minimum, probably raise for keepdims=False and warn for keepdims=True, or in an ideal world raise for both with a TypeError?
Minimal Complete Verifiable Example
outputs
Steps to reproduce
Run the script above
MVCE confirmation
Relevant log output
Anything else we need to know?
I think this this is related to #6772.
I'll take a crack at solving this when I get some time
Environment
Details
INSTALLED VERSIONS
commit: None
python: 3.14.6 | packaged by conda-forge | (main, Aug 11 2026, 10:24:52) [Clang 20.1.8 ]
python-bits: 64
OS: Darwin
OS-release: 24.5.0
machine: arm64
processor: arm
byteorder: little
LC_ALL: None
LANG: en_AU.UTF-8
LOCALE: ('en_AU', 'UTF-8')
libhdf5: None
libnetcdf: None
xarray: 2026.7.0
pandas: 3.0.5
numpy: 2.5.2
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: 3.11.1
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: None
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None
Where this gets a bit weirder
(This is the stuff that grew out of #11519, which I thought was originally a docs mismatch and now looks more like a related family of bugs - works on all reductions on groupbys)
For brevity, I'm going to keep the code snippets shorter - but all of these are in that same environment as above. We'll use a slightly more realistic dataset:
To be concrete, the following are on
type(ds.groupby("time.month"))==<class 'xarray.core.groupby.DatasetGroupBy'>Firstly, as expected:
Now if we specify
keepdims=True:So in this instance, keepdims=True has:
This also works without the kind of mapping approach (I know this is dumb and unlikely but just for completeness):
This seems at odds with what happens with
DatasetRolling.mean()ignoring thekeepdimsFlag (which feels like a very benign bug to me - it's kinda meaningless to set keepdims to False with it), but I think it's also meaningfully an actual bug? Definitely a bit of an edge case - but the results don't really make sense as they stand. I think fix for this second issue with DatasetGroupBy would be to either:groupby()...I can see cases for either - both make sense to me. For consistency with the rolling version, I think explicitly disallowing it for both would be the way to go? It doesn't make a great deal of sense for either & does weird things.