Skip to content

Commit

Permalink
Fully deprecate .drop (#8497)
Browse files Browse the repository at this point in the history
* Fully deprecate `.drop`

I think it's time...
  • Loading branch information
max-sixty authored Dec 2, 2023
1 parent 5213f0d commit 2f00913
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/max-sixty>`_.
- :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
~~~~~~~~~
Expand Down
22 changes: 10 additions & 12 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

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

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

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

0 comments on commit 2f00913

Please sign in to comment.