Skip to content

Commit

Permalink
warn if dim is passed to rolling operations. (#3513)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
dcherian and max-sixty authored Nov 13, 2019
1 parent 94525bb commit 7241aa1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ Bug fixes
By `Deepak Cherian <https://github.com/dcherian>`_.
- Fix error in concatenating unlabeled dimensions (:pull:`3362`).
By `Deepak Cherian <https://github.com/dcherian/>`_.
- Warn if the ``dim`` kwarg is passed to rolling operations. This is redundant since a dimension is
specified when the :py:class:`DatasetRolling` or :py:class:`DataArrayRolling` object is created.
(:pull:`3362`). By `Deepak Cherian <https://github.com/dcherian/>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
9 changes: 9 additions & 0 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import warnings
from typing import Callable

import numpy as np
Expand Down Expand Up @@ -351,6 +352,14 @@ def _bottleneck_reduce(self, func, **kwargs):
def _numpy_or_bottleneck_reduce(
self, array_agg_func, bottleneck_move_func, **kwargs
):
if "dim" in kwargs:
warnings.warn(
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.",
DeprecationWarning,
stacklevel=3,
)
del kwargs["dim"]

if bottleneck_move_func is not None and not isinstance(
self.obj.data, dask_array_type
):
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4188,6 +4188,9 @@ def test_rolling_wrapped_bottleneck(da, name, center, min_periods):
)
assert_array_equal(actual.values, expected)

with pytest.warns(DeprecationWarning, match="Reductions will be applied"):
getattr(rolling_obj, name)(dim="time")

# Test center
rolling_obj = da.rolling(time=7, center=center)
actual = getattr(rolling_obj, name)()["time"]
Expand All @@ -4203,6 +4206,9 @@ def test_rolling_wrapped_dask(da_dask, name, center, min_periods, window):
# dask version
rolling_obj = da_dask.rolling(time=window, min_periods=min_periods, center=center)
actual = getattr(rolling_obj, name)().load()
if name != "count":
with pytest.warns(DeprecationWarning, match="Reductions will be applied"):
getattr(rolling_obj, name)(dim="time")
# numpy version
rolling_obj = da_dask.load().rolling(
time=window, min_periods=min_periods, center=center
Expand Down

0 comments on commit 7241aa1

Please sign in to comment.