From 2f0091393d187e26515ce1274a32caf1074415db Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Fri, 1 Dec 2023 21:52:49 -0800 Subject: [PATCH] Fully deprecate `.drop` (#8497) * Fully deprecate `.drop` I think it's time... --- doc/whats-new.rst | 4 ++++ xarray/core/dataset.py | 22 ++++++++++------------ xarray/tests/test_dataset.py | 12 ++++++------ 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 102a64af433..2722ca0b38a 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -51,6 +51,10 @@ Deprecations currently ``PendingDeprecationWarning``, which are silenced by default. We'll convert these to ``DeprecationWarning`` in a future release. By `Maximilian Roos `_. +- :py:meth:`Dataset.drop` & + :py:meth:`DataArray.drop` are now deprecated, since pending deprecation for + several years. :py:meth:`DataArray.drop_sel` & :py:meth:`DataArray.drop_var` + replace them for labels & variables respectively. Bug fixes ~~~~~~~~~ diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index d010bfbade0..b8093d3dd78 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -111,6 +111,7 @@ decode_numpy_dict_values, drop_dims_from_indexers, either_dict_or_kwargs, + emit_user_level_warning, infix_dims, is_dict_like, is_scalar, @@ -5944,10 +5945,9 @@ def drop( raise ValueError('errors must be either "raise" or "ignore"') if is_dict_like(labels) and not isinstance(labels, dict): - warnings.warn( - "dropping coordinates using `drop` is be deprecated; use drop_vars.", - FutureWarning, - stacklevel=2, + emit_user_level_warning( + "dropping coordinates using `drop` is deprecated; use drop_vars.", + DeprecationWarning, ) return self.drop_vars(labels, errors=errors) @@ -5957,10 +5957,9 @@ def drop( labels = either_dict_or_kwargs(labels, labels_kwargs, "drop") if dim is None and (is_scalar(labels) or isinstance(labels, Iterable)): - warnings.warn( - "dropping variables using `drop` will be deprecated; using drop_vars is encouraged.", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "dropping variables using `drop` is deprecated; use drop_vars.", + DeprecationWarning, ) return self.drop_vars(labels, errors=errors) if dim is not None: @@ -5972,10 +5971,9 @@ def drop( ) return self.drop_sel({dim: labels}, errors=errors, **labels_kwargs) - warnings.warn( - "dropping labels using `drop` will be deprecated; using drop_sel is encouraged.", - PendingDeprecationWarning, - stacklevel=2, + emit_user_level_warning( + "dropping labels using `drop` is deprecated; use `drop_sel` instead.", + DeprecationWarning, ) return self.drop_sel(labels, errors=errors) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index a53d81e36af..37ddcf2786a 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2651,19 +2651,19 @@ def test_drop_variables(self) -> None: # deprecated approach with `drop` works (straight copy paste from above) - with pytest.warns(PendingDeprecationWarning): + with pytest.warns(DeprecationWarning): actual = data.drop("not_found_here", errors="ignore") assert_identical(data, actual) - with pytest.warns(PendingDeprecationWarning): + with pytest.warns(DeprecationWarning): actual = data.drop(["not_found_here"], errors="ignore") assert_identical(data, actual) - with pytest.warns(PendingDeprecationWarning): + with pytest.warns(DeprecationWarning): actual = data.drop(["time", "not_found_here"], errors="ignore") assert_identical(expected, actual) - with pytest.warns(PendingDeprecationWarning): + with pytest.warns(DeprecationWarning): actual = data.drop({"time", "not_found_here"}, errors="ignore") assert_identical(expected, actual) @@ -2736,9 +2736,9 @@ def test_drop_labels_by_keyword(self) -> None: ds5 = data.drop_sel(x=["a", "b"], y=range(0, 6, 2)) arr = DataArray(range(3), dims=["c"]) - with pytest.warns(FutureWarning): + with pytest.warns(DeprecationWarning): data.drop(arr.coords) - with pytest.warns(FutureWarning): + with pytest.warns(DeprecationWarning): data.drop(arr.xindexes) assert_array_equal(ds1.coords["x"], ["b"])