-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add drop_isel #4819
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
Add drop_isel #4819
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4053,13 +4053,78 @@ def drop_sel(self, labels=None, *, errors="raise", **labels_kwargs): | |||||
labels_for_dim = [labels_for_dim] | ||||||
labels_for_dim = np.asarray(labels_for_dim) | ||||||
try: | ||||||
index = self.indexes[dim] | ||||||
index = self.get_index(dim) | ||||||
except KeyError: | ||||||
raise ValueError("dimension %r does not have coordinate labels" % dim) | ||||||
new_index = index.drop(labels_for_dim, errors=errors) | ||||||
ds = ds.loc[{dim: new_index}] | ||||||
return ds | ||||||
|
||||||
def drop_isel(self, indexers=None, **indexers_kwargs): | ||||||
"""Drop index positions from this Dataset. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
indexers : mapping of hashable to Any | ||||||
Index locations to drop | ||||||
**indexers_kwargs : {dim: position, ...}, optional | ||||||
The keyword arguments form of ``dim`` and ``positions`` | ||||||
|
||||||
Returns | ||||||
------- | ||||||
dropped : Dataset | ||||||
|
||||||
Raises | ||||||
------ | ||||||
IndexError | ||||||
|
||||||
Examples | ||||||
-------- | ||||||
>>> data = np.arange(6).reshape(2, 3) | ||||||
>>> labels = ["a", "b", "c"] | ||||||
>>> ds = xr.Dataset({"A": (["x", "y"], data), "y": labels}) | ||||||
mesejo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
>>> 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_isel(y=[0, 2]) | ||||||
<xarray.Dataset> | ||||||
Dimensions: (x: 2, y: 1) | ||||||
Coordinates: | ||||||
* y (y) <U1 'b' | ||||||
Dimensions without coordinates: x | ||||||
Data variables: | ||||||
A (x, y) int64 1 4 | ||||||
>>> ds.drop_isel(y=1) | ||||||
<xarray.Dataset> | ||||||
Dimensions: (x: 2, y: 2) | ||||||
Coordinates: | ||||||
* y (y) <U1 'a' 'c' | ||||||
Dimensions without coordinates: x | ||||||
Data variables: | ||||||
A (x, y) int64 0 2 3 5 | ||||||
""" | ||||||
|
||||||
indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "drop") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one we should change — @mesejo would you be up for putting the one-line PR in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I can do it. No problem, but just to be sure, Should I change it also? BTW There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good spot, that's a mistake, it should be We're trying to move away from random values in the docstrings, but it's not urgent — if you can that would be appreciated, but correcting this & #4819 (comment) is fine to do alone. Thank you! |
||||||
|
||||||
ds = self | ||||||
dimension_index = {} | ||||||
for dim, pos_for_dim in indexers.items(): | ||||||
# Don't cast to set, as it would harm performance when labels | ||||||
# is a large numpy array | ||||||
if utils.is_scalar(pos_for_dim): | ||||||
pos_for_dim = [pos_for_dim] | ||||||
pos_for_dim = np.asarray(pos_for_dim) | ||||||
index = self.get_index(dim) | ||||||
new_index = index.delete(pos_for_dim) | ||||||
dimension_index[dim] = new_index | ||||||
ds = ds.loc[dimension_index] | ||||||
return ds | ||||||
|
||||||
def drop_dims( | ||||||
self, drop_dims: Union[Hashable, Iterable[Hashable]], *, errors: str = "raise" | ||||||
) -> "Dataset": | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2327,6 +2327,12 @@ def test_drop_index_labels(self): | |||||
with pytest.warns(DeprecationWarning): | ||||||
arr.drop([0, 1, 3], dim="y", errors="ignore") | ||||||
|
||||||
def test_drop_index_positions(self): | ||||||
arr = DataArray(np.random.randn(2, 3), dims=["x", "y"]) | ||||||
actual = arr.drop_sel(y=[0, 1]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it intentional that you use
Suggested change
|
||||||
expected = arr[:, 2:] | ||||||
assert_identical(actual, expected) | ||||||
|
||||||
def test_dropna(self): | ||||||
x = np.random.randn(4, 4) | ||||||
x[::2, 0] = np.nan | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.