diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 3abeff78a77..924dbb77ee9 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -143,6 +143,14 @@ Deprecations :py:attr:`DataTree.lineage` warns once instead of twice (:issue:`11721`, :pull:`11724`). By `Daisy DAI `_. +- :py:meth:`Dataset.apply`, + :py:meth:`core.groupby.DataArrayGroupBy.apply`, + :py:meth:`core.groupby.DatasetGroupBy.apply`, + :py:meth:`core.resample.DataArrayResample.apply` and + :py:meth:`core.resample.DatasetResample.apply` now emit a ``FutureWarning`` + instead of ``PendingDeprecationWarning``. Use ``.map`` instead (:issue:`11721`, + :pull:`11784`). + By `Anjali Kashide `_. Bug Fixes diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 7976a8b5f62..122211745b0 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -7155,10 +7155,9 @@ def apply( -------- Dataset.map """ - warnings.warn( - "Dataset.apply may be deprecated in the future. Using Dataset.map is encouraged", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "Dataset.apply is deprecated, use Dataset.map instead.", + FutureWarning, ) return self.map(func, keep_attrs, args, **kwargs) diff --git a/xarray/core/groupby.py b/xarray/core/groupby.py index 3ee4430a28b..d5078e1beb0 100644 --- a/xarray/core/groupby.py +++ b/xarray/core/groupby.py @@ -1738,10 +1738,9 @@ def apply(self, func, shortcut=False, args=(), **kwargs): -------- DataArrayGroupBy.map """ - warnings.warn( - "GroupBy.apply may be deprecated in the future. Using GroupBy.map is encouraged", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "GroupBy.apply is deprecated, use GroupBy.map instead.", + FutureWarning, ) return self.map(func, shortcut=shortcut, args=args, **kwargs) @@ -1918,10 +1917,9 @@ def apply(self, func, args=(), shortcut=None, **kwargs): -------- DatasetGroupBy.map """ - warnings.warn( - "GroupBy.apply may be deprecated in the future. Using GroupBy.map is encouraged", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "GroupBy.apply is deprecated, use GroupBy.map instead.", + FutureWarning, ) return self.map(func, shortcut=shortcut, args=args, **kwargs) diff --git a/xarray/core/resample.py b/xarray/core/resample.py index 61b783368cc..49b4ef2d247 100644 --- a/xarray/core/resample.py +++ b/xarray/core/resample.py @@ -1,6 +1,5 @@ from __future__ import annotations -import warnings from collections.abc import Callable, Hashable, Iterable, Sequence from typing import TYPE_CHECKING, Any, Literal, override @@ -10,6 +9,7 @@ ) from xarray.core.groupby import DataArrayGroupByBase, DatasetGroupByBase, GroupBy from xarray.core.types import Dims, InterpOptions, T_Xarray +from xarray.core.utils import emit_user_level_warning if TYPE_CHECKING: from xarray.core.dataarray import DataArray @@ -382,10 +382,9 @@ def apply(self, func, args=(), shortcut=None, **kwargs): -------- DataArrayResample.map """ - warnings.warn( - "Resample.apply may be deprecated in the future. Using Resample.map is encouraged", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "Resample.apply is deprecated, use Resample.map instead.", + FutureWarning, ) return self.map(func=func, shortcut=shortcut, args=args, **kwargs) @@ -469,10 +468,9 @@ def apply(self, func, args=(), shortcut=None, **kwargs): -------- DataSetResample.map """ - warnings.warn( - "Resample.apply may be deprecated in the future. Using Resample.map is encouraged", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "Resample.apply is deprecated, use Resample.map instead.", + FutureWarning, ) return self.map(func=func, shortcut=shortcut, args=args, **kwargs) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index a0887c5b9cb..127f4e61c5b 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -6844,11 +6844,11 @@ def modify_attr(da): result = ds.map(modify_attr, keep_attrs=False) assert result["test"].attrs == {"original": "modified", "extra": "added"} - def test_apply_pending_deprecated_map(self) -> None: + def test_apply_deprecated_map(self) -> None: data = create_test_data() data.attrs["foo"] = "bar" - with pytest.warns(PendingDeprecationWarning): + with pytest.warns(FutureWarning, match=r"Dataset\.apply is deprecated"): # data.apply keeps all attrs by default assert_identical(data.apply(np.mean), data.mean()) diff --git a/xarray/tests/test_groupby.py b/xarray/tests/test_groupby.py index 25c5c4ba4f3..8e50f8d08a1 100644 --- a/xarray/tests/test_groupby.py +++ b/xarray/tests/test_groupby.py @@ -274,6 +274,33 @@ def func(arg1, arg2, arg3=0): assert_identical(expected, actual) +@pytest.mark.parametrize("use_da", [True, False]) +def test_groupby_apply_deprecated(use_da: bool) -> None: + obj: xr.DataArray | xr.Dataset = xr.DataArray( + [1.0, 2.0, 3.0, 4.0], [("x", [1, 1, 2, 2])] + ) + if not use_da: + obj = obj.to_dataset(name="foo") + + expected = obj.groupby("x").map(lambda g: g - g.mean()) + with pytest.warns(FutureWarning, match=r"GroupBy\.apply is deprecated"): + actual = obj.groupby("x").apply(lambda g: g - g.mean()) + assert_identical(expected, actual) + + +@pytest.mark.parametrize("use_da", [True, False]) +def test_resample_apply_deprecated(use_da: bool) -> None: + times = pd.date_range("2000-01-01", freq="6h", periods=8) + obj: xr.DataArray | xr.Dataset = xr.DataArray(np.arange(8.0), [("time", times)]) + if not use_da: + obj = obj.to_dataset(name="foo") + + expected = obj.resample(time="1D").map(lambda g: g.mean("time")) + with pytest.warns(FutureWarning, match=r"Resample\.apply is deprecated"): + actual = obj.resample(time="1D").apply(lambda g: g.mean("time")) + assert_identical(expected, actual) + + def test_da_groupby_empty() -> None: empty_array = xr.DataArray([], dims="dim")