Skip to content

Commit

Permalink
Warn ignored keep attrs (#5265)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathause authored May 6, 2021
1 parent 60e3963 commit 477fc2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ Bug fixes
``np.datetime64[ns]`` dates with or without ``cftime`` installed
(:issue:`5093`, :pull:`5180`). By `Spencer Clark
<https://github.com/spencerkclark>`_.
- Warn on passing ``keep_attrs`` to ``resample`` and ``rolling_exp`` as they are ignored, pass ``keep_attrs``
to the applied function instead (:pull:`5265`). By `Mathias Hauser <https://github.com/mathause>`_.


Documentation
~~~~~~~~~~~~~
Expand Down
20 changes: 14 additions & 6 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,14 @@ def rolling_exp(
--------
core.rolling_exp.RollingExp
"""

if "keep_attrs" in window_kwargs:
warnings.warn(
"Passing ``keep_attrs`` to ``rolling_exp`` has no effect. Pass"
" ``keep_attrs`` directly to the applied function, e.g."
" ``rolling_exp(...).mean(keep_attrs=False)``."
)

window = either_dict_or_kwargs(window, window_kwargs, "rolling_exp")

return RollingExp(self, window, window_type)
Expand Down Expand Up @@ -1045,10 +1053,6 @@ def resample(
loffset : timedelta or str, optional
Offset used to adjust the resampled time labels. Some pandas date
offset strings are supported.
keep_attrs : bool, optional
If True, the object's attributes (`attrs`) will be copied from
the original object to the new one. If False (default), the new
object will be returned without attributes.
restore_coord_dims : bool, optional
If True, also restore the dimension order of multi-dimensional
coordinates.
Expand Down Expand Up @@ -1123,8 +1127,12 @@ def resample(
from .dataarray import DataArray
from .resample import RESAMPLE_DIM

if keep_attrs is None:
keep_attrs = _get_keep_attrs(default=False)
if keep_attrs is not None:
warnings.warn(
"Passing ``keep_attrs`` to ``resample`` has no effect and will raise an"
" error in xarray 0.20. Pass ``keep_attrs`` directly to the applied"
" function, e.g. ``resample(...).mean(keep_attrs=True)``."
)

# note: the second argument (now 'skipna') use to be 'dim'
if (
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3093,6 +3093,11 @@ def test_resample_keep_attrs(self):
expected = DataArray([1, 1, 1], [("time", times[::4])], attrs=array.attrs)
assert_identical(result, expected)

with pytest.warns(
UserWarning, match="Passing ``keep_attrs`` to ``resample`` has no effect."
):
array.resample(time="1D", keep_attrs=True)

def test_resample_skipna(self):
times = pd.date_range("2000-01-01", freq="6H", periods=10)
array = DataArray(np.ones(10), [("time", times)])
Expand Down Expand Up @@ -7322,6 +7327,11 @@ def test_rolling_exp_keep_attrs(da):
result = da.rolling_exp(time=10).mean(keep_attrs=False)
assert result.attrs == {}

with pytest.warns(
UserWarning, match="Passing ``keep_attrs`` to ``rolling_exp`` has no effect."
):
da.rolling_exp(time=10, keep_attrs=True)


def test_no_dict():
d = DataArray()
Expand Down
10 changes: 10 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3947,6 +3947,11 @@ def test_resample_by_mean_with_keep_attrs(self):
expected = ds.attrs
assert expected == actual

with pytest.warns(
UserWarning, match="Passing ``keep_attrs`` to ``resample`` has no effect."
):
ds.resample(time="1D", keep_attrs=True)

def test_resample_loffset(self):
times = pd.date_range("2000-01-01", freq="6H", periods=10)
ds = Dataset(
Expand Down Expand Up @@ -6541,6 +6546,11 @@ def test_rolling_exp_keep_attrs(ds):
assert result.attrs == {}
assert result.z1.attrs == {}

with pytest.warns(
UserWarning, match="Passing ``keep_attrs`` to ``rolling_exp`` has no effect."
):
ds.rolling_exp(time=10, keep_attrs=True)


@pytest.mark.parametrize("center", (True, False))
@pytest.mark.parametrize("min_periods", (None, 1, 2, 3))
Expand Down

0 comments on commit 477fc2f

Please sign in to comment.