Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn ignored keep attrs #5265

Merged
merged 3 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:`5180`). By `Mathias Hauser <https://github.com/mathause>`_.
mathause marked this conversation as resolved.
Show resolved Hide resolved


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:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if I should warn here. Nobody ever complained. However, all other functions either warn (from a deprection) or error (because keep_atttrs is not a valid keyword).

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