Skip to content

Commit 477fc2f

Browse files
authored
Warn ignored keep attrs (#5265)
1 parent 60e3963 commit 477fc2f

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ Bug fixes
188188
``np.datetime64[ns]`` dates with or without ``cftime`` installed
189189
(:issue:`5093`, :pull:`5180`). By `Spencer Clark
190190
<https://github.com/spencerkclark>`_.
191+
- Warn on passing ``keep_attrs`` to ``resample`` and ``rolling_exp`` as they are ignored, pass ``keep_attrs``
192+
to the applied function instead (:pull:`5265`). By `Mathias Hauser <https://github.com/mathause>`_.
193+
191194

192195
Documentation
193196
~~~~~~~~~~~~~

xarray/core/common.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,14 @@ def rolling_exp(
926926
--------
927927
core.rolling_exp.RollingExp
928928
"""
929+
930+
if "keep_attrs" in window_kwargs:
931+
warnings.warn(
932+
"Passing ``keep_attrs`` to ``rolling_exp`` has no effect. Pass"
933+
" ``keep_attrs`` directly to the applied function, e.g."
934+
" ``rolling_exp(...).mean(keep_attrs=False)``."
935+
)
936+
929937
window = either_dict_or_kwargs(window, window_kwargs, "rolling_exp")
930938

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

1126-
if keep_attrs is None:
1127-
keep_attrs = _get_keep_attrs(default=False)
1130+
if keep_attrs is not None:
1131+
warnings.warn(
1132+
"Passing ``keep_attrs`` to ``resample`` has no effect and will raise an"
1133+
" error in xarray 0.20. Pass ``keep_attrs`` directly to the applied"
1134+
" function, e.g. ``resample(...).mean(keep_attrs=True)``."
1135+
)
11281136

11291137
# note: the second argument (now 'skipna') use to be 'dim'
11301138
if (

xarray/tests/test_dataarray.py

+10
Original file line numberDiff line numberDiff line change
@@ -3093,6 +3093,11 @@ def test_resample_keep_attrs(self):
30933093
expected = DataArray([1, 1, 1], [("time", times[::4])], attrs=array.attrs)
30943094
assert_identical(result, expected)
30953095

3096+
with pytest.warns(
3097+
UserWarning, match="Passing ``keep_attrs`` to ``resample`` has no effect."
3098+
):
3099+
array.resample(time="1D", keep_attrs=True)
3100+
30963101
def test_resample_skipna(self):
30973102
times = pd.date_range("2000-01-01", freq="6H", periods=10)
30983103
array = DataArray(np.ones(10), [("time", times)])
@@ -7322,6 +7327,11 @@ def test_rolling_exp_keep_attrs(da):
73227327
result = da.rolling_exp(time=10).mean(keep_attrs=False)
73237328
assert result.attrs == {}
73247329

7330+
with pytest.warns(
7331+
UserWarning, match="Passing ``keep_attrs`` to ``rolling_exp`` has no effect."
7332+
):
7333+
da.rolling_exp(time=10, keep_attrs=True)
7334+
73257335

73267336
def test_no_dict():
73277337
d = DataArray()

xarray/tests/test_dataset.py

+10
Original file line numberDiff line numberDiff line change
@@ -3947,6 +3947,11 @@ def test_resample_by_mean_with_keep_attrs(self):
39473947
expected = ds.attrs
39483948
assert expected == actual
39493949

3950+
with pytest.warns(
3951+
UserWarning, match="Passing ``keep_attrs`` to ``resample`` has no effect."
3952+
):
3953+
ds.resample(time="1D", keep_attrs=True)
3954+
39503955
def test_resample_loffset(self):
39513956
times = pd.date_range("2000-01-01", freq="6H", periods=10)
39523957
ds = Dataset(
@@ -6541,6 +6546,11 @@ def test_rolling_exp_keep_attrs(ds):
65416546
assert result.attrs == {}
65426547
assert result.z1.attrs == {}
65436548

6549+
with pytest.warns(
6550+
UserWarning, match="Passing ``keep_attrs`` to ``rolling_exp`` has no effect."
6551+
):
6552+
ds.rolling_exp(time=10, keep_attrs=True)
6553+
65446554

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

0 commit comments

Comments
 (0)