Skip to content

Commit e0fd480

Browse files
authored
allow passing any iterable to drop when dropping variables (pydata#3693)
* allow passing any iterable to drop when dropping variables * whats-new.rst * update whats-new.rst
1 parent 4042345 commit e0fd480

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ Bug fixes
7676
MultiIndex level names.
7777
- :py:meth:`Dataset.merge` no longer fails when passed a `DataArray` instead of a `Dataset` object.
7878
By `Tom Nicholas <https://github.com/TomNicholas>`_.
79+
- Fix a regression in :py:meth:`Dataset.drop`: allow passing any
80+
iterable when dropping variables (:issue:`3552`, :pull:`3693`)
81+
By `Justus Magin <https://github.com/keewis>`_.
7982

8083
Documentation
8184
~~~~~~~~~~~~~

xarray/core/dataset.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
either_dict_or_kwargs,
8686
hashable,
8787
is_dict_like,
88-
is_list_like,
8988
is_scalar,
9089
maybe_wrap_array,
9190
)
@@ -3690,7 +3689,7 @@ def drop(self, labels=None, dim=None, *, errors="raise", **labels_kwargs):
36903689
raise ValueError("cannot specify dim and dict-like arguments.")
36913690
labels = either_dict_or_kwargs(labels, labels_kwargs, "drop")
36923691

3693-
if dim is None and (is_list_like(labels) or is_scalar(labels)):
3692+
if dim is None and (is_scalar(labels) or isinstance(labels, Iterable)):
36943693
warnings.warn(
36953694
"dropping variables using `drop` will be deprecated; using drop_vars is encouraged.",
36963695
PendingDeprecationWarning,

xarray/tests/test_dataset.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,6 +2167,10 @@ def test_drop_variables(self):
21672167
actual = data.drop(["time", "not_found_here"], errors="ignore")
21682168
assert_identical(expected, actual)
21692169

2170+
with pytest.warns(PendingDeprecationWarning):
2171+
actual = data.drop({"time", "not_found_here"}, errors="ignore")
2172+
assert_identical(expected, actual)
2173+
21702174
def test_drop_index_labels(self):
21712175
data = Dataset({"A": (["x", "y"], np.random.randn(2, 3)), "x": ["a", "b"]})
21722176

0 commit comments

Comments
 (0)