-
-
Couldn't load subscription status.
- Fork 1.2k
Fix drop_sel for a MultiIndex
#10863
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
Changes from all commits
d288643
4eb40ab
7b09905
f1089d0
22aadff
293fa1f
ca372a1
7d23fed
8950155
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 | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6065,6 +6065,8 @@ def drop_sel( | |||||||||||||||||||||||||||||||||
| Data variables: | ||||||||||||||||||||||||||||||||||
| A (x, y) int64 32B 0 2 3 5 | ||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||
| from xarray.core.dataarray import DataArray | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if errors not in ["raise", "ignore"]: | ||||||||||||||||||||||||||||||||||
| raise ValueError('errors must be either "raise" or "ignore"') | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -6076,7 +6078,11 @@ def drop_sel( | |||||||||||||||||||||||||||||||||
| # is a large numpy array | ||||||||||||||||||||||||||||||||||
| if utils.is_scalar(labels_for_dim): | ||||||||||||||||||||||||||||||||||
| labels_for_dim = [labels_for_dim] | ||||||||||||||||||||||||||||||||||
| labels_for_dim = np.asarray(labels_for_dim) | ||||||||||||||||||||||||||||||||||
| # Most conversion to arrays is better handled in the indexer, however | ||||||||||||||||||||||||||||||||||
| # DataArrays are a special case where the underlying libraries don't provide | ||||||||||||||||||||||||||||||||||
| # a good conversition. | ||||||||||||||||||||||||||||||||||
| if isinstance(labels_for_dim, DataArray): | ||||||||||||||||||||||||||||||||||
| labels_for_dim = np.asarray(labels_for_dim) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+6081
to
+6085
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. If you wanted to make this a little safer, could add:
Suggested change
But this LGTM to me! Definitely an incremental improvement. 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. Commit and then reverted, highlighed by the tests it might break peoples current usage where a DataArray gets assigned the default dim names (i.e Also despite thinking this would nudge >>> data = xr.Dataset({"x": ["a", "b"]})
>>> data.sel(x=xr.DataArray(["a",], dims=("y",)))
<xarray.Dataset> Size: 4B
Dimensions: (y: 1)
Coordinates:
x (y) <U1 4B 'a'
Dimensions without coordinates: y
Data variables:
*empty*So would propose leaving this change for now. 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. Right, sel() imposes the dimensions and coordinates of the indexer rather than checking for alignment. It is not obvious to me what the inverse of that would be! |
||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||
| index = self.get_index(dim) | ||||||||||||||||||||||||||||||||||
| except KeyError as err: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've imported
DataArrayhere as that seems to fit with the style in other parts of the codebase (here), however I'm not entirely sure why this is done.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that
dataarray.pyimportsdataset.pyalready (historically Dataset predates DataArray slightly), so this avoids a recursive import.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks for the explanation👍