Skip to content

Commit 487b087

Browse files
gwgundersenmax-sixty
authored andcommitted
Drop keyword support: small fixes (#3233)
* Amended docs for new API and deprecation warnings; deprecated dropping DataArrayCoordinates (#2910). * Removed okwarning now that docs have non-deprecated API. * More clear warnings and more tests. * Moved both warnings to top of function for code clarity. * Removed unused imports. * Update dataset.py
1 parent 79dc7dc commit 487b087

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

doc/indexing.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,8 @@ The :py:meth:`~xarray.Dataset.drop` method returns a new object with the listed
236236
index labels along a dimension dropped:
237237

238238
.. ipython:: python
239-
:okwarning:
240239
241-
ds.drop(['IN', 'IL'], dim='space')
240+
ds.drop(space=['IN', 'IL'])
242241
243242
``drop`` is both a ``Dataset`` and ``DataArray`` method.
244243

doc/whats-new.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ Enhancements
6969
By `Ulrich Herter <https://github.com/ulijh>`_.
7070

7171
- :py:meth:`~xarray.Dataset.drop` now supports keyword arguments; dropping index
72-
labels by specifying both ``dim`` and ``labels`` is deprecated (:issue:`2910`).
72+
labels by using both ``dim`` and ``labels`` or using a
73+
:py:class:`~xarray.core.coordinates.DataArrayCoordinates` object are
74+
deprecated (:issue:`2910`).
7375
By `Gregory Gundersen <https://github.com/gwgundersen/>`_.
7476

7577
- Added examples of :py:meth:`Dataset.set_index` and

xarray/core/dataset.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
_contains_datetime_like_objects,
5656
)
5757
from .coordinates import (
58-
DataArrayCoordinates,
5958
DatasetCoordinates,
6059
LevelCoordinatesSource,
6160
assert_coordinate_consistent,
@@ -79,6 +78,8 @@
7978
either_dict_or_kwargs,
8079
hashable,
8180
maybe_wrap_array,
81+
is_dict_like,
82+
is_list_like,
8283
)
8384
from .variable import IndexVariable, Variable, as_variable, broadcast_variables
8485

@@ -3555,9 +3556,23 @@ def drop( # noqa: F811
35553556
if errors not in ["raise", "ignore"]:
35563557
raise ValueError('errors must be either "raise" or "ignore"')
35573558

3558-
labels_are_coords = isinstance(labels, DataArrayCoordinates)
3559-
if labels_kwargs or (utils.is_dict_like(labels) and not labels_are_coords):
3560-
labels_kwargs = utils.either_dict_or_kwargs(labels, labels_kwargs, "drop")
3559+
if is_dict_like(labels) and not isinstance(labels, dict):
3560+
warnings.warn(
3561+
"dropping coordinates using key values of dict-like labels is "
3562+
"deprecated; use drop_vars or a list of coordinates.",
3563+
FutureWarning,
3564+
stacklevel=2,
3565+
)
3566+
if dim is not None and is_list_like(labels):
3567+
warnings.warn(
3568+
"dropping dimensions using list-like labels is deprecated; use "
3569+
"dict-like arguments.",
3570+
DeprecationWarning,
3571+
stacklevel=2,
3572+
)
3573+
3574+
if labels_kwargs or isinstance(labels, dict):
3575+
labels_kwargs = either_dict_or_kwargs(labels, labels_kwargs, "drop")
35613576
if dim is not None:
35623577
raise ValueError("cannot specify dim and dict-like arguments.")
35633578
ds = self
@@ -3571,13 +3586,6 @@ def drop( # noqa: F811
35713586
labels = set(labels)
35723587
return self._drop_vars(labels, errors=errors)
35733588
else:
3574-
if utils.is_list_like(labels):
3575-
warnings.warn(
3576-
"dropping dimensions using list-like labels is deprecated; "
3577-
"use dict-like arguments.",
3578-
DeprecationWarning,
3579-
stacklevel=2,
3580-
)
35813589
return self._drop_labels(labels, dim, errors=errors)
35823590

35833591
def _drop_labels(self, labels=None, dim=None, errors="raise"):

xarray/tests/test_dataset.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,14 +2214,21 @@ def test_drop_labels_by_keyword(self):
22142214
# Basic functionality.
22152215
assert len(data.coords["x"]) == 2
22162216

2217-
# This API is allowed but deprecated.
2217+
# In the future, this will break.
22182218
with pytest.warns(DeprecationWarning):
22192219
ds1 = data.drop(["a"], dim="x")
22202220
ds2 = data.drop(x="a")
22212221
ds3 = data.drop(x=["a"])
22222222
ds4 = data.drop(x=["a", "b"])
22232223
ds5 = data.drop(x=["a", "b"], y=range(0, 6, 2))
22242224

2225+
# In the future, this will result in different behavior.
2226+
arr = DataArray(range(3), dims=["c"])
2227+
with pytest.warns(FutureWarning):
2228+
data.drop(arr.coords)
2229+
with pytest.warns(FutureWarning):
2230+
data.drop(arr.indexes)
2231+
22252232
assert_array_equal(ds1.coords["x"], ["b"])
22262233
assert_array_equal(ds2.coords["x"], ["b"])
22272234
assert_array_equal(ds3.coords["x"], ["b"])

0 commit comments

Comments
 (0)