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

Fully deprecate .drop #8497

Merged
merged 3 commits into from
Dec 2, 2023
Merged
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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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 @@ -110,6 +110,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 @@ -5942,10 +5943,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 @@ -5955,10 +5955,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 @@ -5970,10 +5969,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
Loading