-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
sel along 1D non-index coordinates #3925
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,27 +205,35 @@ def convert_label_indexer(index, label, index_name="", method=None, tolerance=No | |
|
||
def get_dim_indexers(data_obj, indexers): | ||
"""Given a xarray data object and label based indexers, return a mapping | ||
of label indexers with only dimension names as keys. | ||
of label indexers with only dimension names (or 1D non-dimensional coord | ||
names) as keys. | ||
|
||
It groups multiple level indexers given on a multi-index dimension | ||
into a single, dictionary indexer for that dimension (Raise a ValueError | ||
if it is not possible). | ||
""" | ||
non_dim_1d_coords = [coord for coord in data_obj.coords | ||
if len(data_obj[coord].dims) == 1] | ||
invalid = [ | ||
k | ||
for k in indexers | ||
if k not in data_obj.dims and k not in data_obj._level_coords | ||
k for k in indexers | ||
if k not in data_obj.dims and k not in data_obj._level_coords | ||
and k not in non_dim_1d_coords | ||
] | ||
if invalid: | ||
raise ValueError(f"dimensions or multi-index levels {invalid!r} do not exist") | ||
if invalid: # TODO This was never covered by testing | ||
raise ValueError(f"dimensions, 1D coordinates, or multi-index levels" | ||
f" {invalid!r} do not exist") | ||
|
||
level_indexers = defaultdict(dict) | ||
dim_indexers = {} | ||
for key, label in indexers.items(): | ||
(dim,) = data_obj[key].dims | ||
if key != dim: | ||
# assume here multi-index level indexer | ||
level_indexers[dim][key] = label | ||
# If key is 1D non-dimension coordinate let it pass through | ||
if key in data_obj.coords and data_obj.coords[key].dims == (dim,): | ||
dim_indexers[key] = label | ||
else: | ||
# assume here multi-index level indexer | ||
level_indexers[dim][key] = label | ||
else: | ||
dim_indexers[key] = label | ||
|
||
|
@@ -253,9 +261,31 @@ def remap_label_indexers(data_obj, indexers, method=None, tolerance=None): | |
|
||
dim_indexers = get_dim_indexers(data_obj, indexers) | ||
for dim, label in dim_indexers.items(): | ||
try: | ||
if dim in data_obj.indexes: | ||
index = data_obj.indexes[dim] | ||
except KeyError: | ||
|
||
coords_dtype = data_obj.coords[dim].dtype | ||
label = maybe_cast_to_coords_dtype(label, coords_dtype) | ||
idxr, new_idx = convert_label_indexer(index, label, dim, | ||
method, tolerance) | ||
pos_indexers[dim] = idxr | ||
if new_idx is not None: | ||
new_indexes[dim] = new_idx | ||
|
||
elif dim in data_obj.coords and len(data_obj.coords[dim] == 1): | ||
# 1D non-dimension coord | ||
index = data_obj.coords[dim].to_index() | ||
(dim,) = data_obj[dim].dims | ||
|
||
coords_dtype = data_obj.coords[dim].dtype | ||
label = maybe_cast_to_coords_dtype(label, coords_dtype) | ||
idxr, new_idx = convert_label_indexer(index, label, dim, | ||
method, tolerance) | ||
pos_indexers[dim] = idxr | ||
if new_idx is not None: | ||
new_indexes[dim] = new_idx | ||
Comment on lines
+280
to
+286
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. Can you refactor so this code block doesn't need to be repeated? Every index is a 1D coordinate, so I think you could do this by switching around the order of these if dim in data_obj.coords and data_obj.coords[dim].ndim == 1:
if dim in data_obj.indexes:
index = data_obj.indexes[dim]
else:
index = data_obj.coords[dim].to_index()
(dim,) = data_obj[dim].dims
coords_dtype = data_obj.coords[dim].dtype
label = maybe_cast_to_coords_dtype(label, coords_dtype)
idxr, new_idx = convert_label_indexer(index, label, dim,
method, tolerance)
pos_indexers[dim] = idxr
if new_idx is not None:
new_indexes[dim] = new_idx |
||
|
||
else: | ||
# no index for this dimension: reuse the provided labels | ||
if method is not None or tolerance is not None: | ||
raise ValueError( | ||
|
@@ -264,13 +294,6 @@ def remap_label_indexers(data_obj, indexers, method=None, tolerance=None): | |
"an associated coordinate." | ||
) | ||
pos_indexers[dim] = label | ||
else: | ||
coords_dtype = data_obj.coords[dim].dtype | ||
label = maybe_cast_to_coords_dtype(label, coords_dtype) | ||
idxr, new_idx = convert_label_indexer(index, label, dim, method, tolerance) | ||
pos_indexers[dim] = idxr | ||
if new_idx is not None: | ||
new_indexes[dim] = new_idx | ||
|
||
return pos_indexers, new_indexes | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this should be
data_obj.coords[dim].ndim == 1
instead oflen(data_obj.coords[dim] == 1)
? The later is checking that the coordinate's first dimension has length 1.