Skip to content

Commit

Permalink
fix issues in drop_sel and drop_isel (#4828)
Browse files Browse the repository at this point in the history
  • Loading branch information
mesejo authored Jan 20, 2021
1 parent 7dbbdca commit 93ea177
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4021,30 +4021,38 @@ def drop_sel(self, labels=None, *, errors="raise", **labels_kwargs):
Examples
--------
>>> data = np.random.randn(2, 3)
>>> data = np.arange(6).reshape(2, 3)
>>> labels = ["a", "b", "c"]
>>> ds = xr.Dataset({"A": (["x", "y"], data), "y": labels})
>>> ds
<xarray.Dataset>
Dimensions: (x: 2, y: 3)
Coordinates:
* y (y) <U1 'a' 'b' 'c'
Dimensions without coordinates: x
Data variables:
A (x, y) int64 0 1 2 3 4 5
>>> ds.drop_sel(y=["a", "c"])
<xarray.Dataset>
Dimensions: (x: 2, y: 1)
Coordinates:
* y (y) <U1 'b'
Dimensions without coordinates: x
Data variables:
A (x, y) float64 0.4002 1.868
A (x, y) int64 1 4
>>> ds.drop_sel(y="b")
<xarray.Dataset>
Dimensions: (x: 2, y: 2)
Coordinates:
* y (y) <U1 'a' 'c'
Dimensions without coordinates: x
Data variables:
A (x, y) float64 1.764 0.9787 2.241 -0.9773
A (x, y) int64 0 2 3 5
"""
if errors not in ["raise", "ignore"]:
raise ValueError('errors must be either "raise" or "ignore"')

labels = either_dict_or_kwargs(labels, labels_kwargs, "drop")
labels = either_dict_or_kwargs(labels, labels_kwargs, "drop_sel")

ds = self
for dim, labels_for_dim in labels.items():
Expand Down Expand Up @@ -4110,7 +4118,7 @@ def drop_isel(self, indexers=None, **indexers_kwargs):
A (x, y) int64 0 2 3 5
"""

indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "drop")
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "drop_isel")

ds = self
dimension_index = {}
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,7 +2329,7 @@ def test_drop_index_labels(self):

def test_drop_index_positions(self):
arr = DataArray(np.random.randn(2, 3), dims=["x", "y"])
actual = arr.drop_sel(y=[0, 1])
actual = arr.drop_isel(y=[0, 1])
expected = arr[:, 2:]
assert_identical(actual, expected)

Expand Down

0 comments on commit 93ea177

Please sign in to comment.