Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ Deprecations
:py:attr:`DataTree.lineage` warns once instead of twice (:issue:`11721`,
:pull:`11724`).
By `Daisy DAI <https://github.com/ShinoKana>`_.
- :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 <https://github.com/Anju522>`_.


Bug Fixes
Expand Down
7 changes: 3 additions & 4 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 6 additions & 8 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
16 changes: 7 additions & 9 deletions xarray/core/resample.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
27 changes: 27 additions & 0 deletions xarray/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading