Skip to content

Commit 7241aa1

Browse files
dcherianmax-sixty
andauthored
warn if dim is passed to rolling operations. (#3513)
* warn if dim is passed to rolling operations. * Update doc/whats-new.rst Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> * Update xarray/core/rolling.py Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com>
1 parent 94525bb commit 7241aa1

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ Bug fixes
220220
By `Deepak Cherian <https://github.com/dcherian>`_.
221221
- Fix error in concatenating unlabeled dimensions (:pull:`3362`).
222222
By `Deepak Cherian <https://github.com/dcherian/>`_.
223+
- Warn if the ``dim`` kwarg is passed to rolling operations. This is redundant since a dimension is
224+
specified when the :py:class:`DatasetRolling` or :py:class:`DataArrayRolling` object is created.
225+
(:pull:`3362`). By `Deepak Cherian <https://github.com/dcherian/>`_.
223226

224227
Documentation
225228
~~~~~~~~~~~~~

xarray/core/rolling.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import warnings
23
from typing import Callable
34

45
import numpy as np
@@ -351,6 +352,14 @@ def _bottleneck_reduce(self, func, **kwargs):
351352
def _numpy_or_bottleneck_reduce(
352353
self, array_agg_func, bottleneck_move_func, **kwargs
353354
):
355+
if "dim" in kwargs:
356+
warnings.warn(
357+
f"Reductions will be applied along the rolling dimension '{self.dim}'. Passing the 'dim' kwarg to reduction operations has no effect and will raise an error in xarray 0.16.0.",
358+
DeprecationWarning,
359+
stacklevel=3,
360+
)
361+
del kwargs["dim"]
362+
354363
if bottleneck_move_func is not None and not isinstance(
355364
self.obj.data, dask_array_type
356365
):

xarray/tests/test_dataarray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,9 @@ def test_rolling_wrapped_bottleneck(da, name, center, min_periods):
41884188
)
41894189
assert_array_equal(actual.values, expected)
41904190

4191+
with pytest.warns(DeprecationWarning, match="Reductions will be applied"):
4192+
getattr(rolling_obj, name)(dim="time")
4193+
41914194
# Test center
41924195
rolling_obj = da.rolling(time=7, center=center)
41934196
actual = getattr(rolling_obj, name)()["time"]
@@ -4203,6 +4206,9 @@ def test_rolling_wrapped_dask(da_dask, name, center, min_periods, window):
42034206
# dask version
42044207
rolling_obj = da_dask.rolling(time=window, min_periods=min_periods, center=center)
42054208
actual = getattr(rolling_obj, name)().load()
4209+
if name != "count":
4210+
with pytest.warns(DeprecationWarning, match="Reductions will be applied"):
4211+
getattr(rolling_obj, name)(dim="time")
42064212
# numpy version
42074213
rolling_obj = da_dask.load().rolling(
42084214
time=window, min_periods=min_periods, center=center

0 commit comments

Comments
 (0)