diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 59857c67bf7..86af41e7a63 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -188,6 +188,9 @@ Bug fixes ``np.datetime64[ns]`` dates with or without ``cftime`` installed (:issue:`5093`, :pull:`5180`). By `Spencer Clark `_. +- 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 `_. + Documentation ~~~~~~~~~~~~~ diff --git a/xarray/core/common.py b/xarray/core/common.py index e348600b0bb..c9386c4e15f 100644 --- a/xarray/core/common.py +++ b/xarray/core/common.py @@ -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) @@ -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. @@ -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 ( diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index d62c156b6d3..ff098ced161 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -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)]) @@ -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() diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index ef8db0374d5..33b5d16fbac 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -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( @@ -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))