-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update concat for multi-variable indexes. #10371
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a996655
Update concat for multi-variable indexes.
dcherian 7979079
Apply suggestions from code review
dcherian c2dfd9a
Refactor out test Index classes
dcherian bb61abd
Cleanup
dcherian 29319e8
Add test
dcherian ce68103
Merge branch 'main' into concat-multi-var-index
dcherian 37e9615
Refactor tests
dcherian 7ad1b14
Update xarray/tests/indexes.py
dcherian da0e938
typing fixes
dcherian ab2bc9f
Merge branch 'main' into concat-multi-var-index
dcherian 0308cf3
more typing fixes
dcherian 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 hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from collections.abc import Hashable, Iterable, Mapping, Sequence | ||
from typing import Any | ||
|
||
import numpy as np | ||
|
||
from xarray import Variable | ||
from xarray.core.indexes import Index, PandasIndex | ||
from xarray.core.types import Self | ||
|
||
|
||
class ScalarIndex(Index): | ||
def __init__(self, value: int): | ||
self.value = value | ||
|
||
@classmethod | ||
def from_variables(cls, variables, *, options) -> Self: | ||
var = next(iter(variables.values())) | ||
return cls(int(var.values)) | ||
|
||
def equals(self, other, *, exclude=None) -> bool: | ||
return isinstance(other, ScalarIndex) and other.value == self.value | ||
|
||
|
||
class XYIndex(Index): | ||
def __init__(self, x: PandasIndex, y: PandasIndex): | ||
self.x: PandasIndex = x | ||
self.y: PandasIndex = y | ||
|
||
@classmethod | ||
def from_variables(cls, variables, *, options): | ||
return cls( | ||
x=PandasIndex.from_variables({"x": variables["x"]}, options=options), | ||
y=PandasIndex.from_variables({"y": variables["y"]}, options=options), | ||
) | ||
|
||
def create_variables( | ||
self, variables: Mapping[Any, Variable] | None = None | ||
) -> dict[Any, Variable]: | ||
return self.x.create_variables() | self.y.create_variables() | ||
|
||
def equals(self, other, exclude=None): | ||
if exclude is None: | ||
exclude = frozenset() | ||
x_eq = True if self.x.dim in exclude else self.x.equals(other.x) | ||
y_eq = True if self.y.dim in exclude else self.y.equals(other.y) | ||
return x_eq and y_eq | ||
|
||
@classmethod | ||
def concat( | ||
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. added this |
||
cls, | ||
indexes: Sequence[Self], | ||
dim: Hashable, | ||
positions: Iterable[Iterable[int]] | None = None, | ||
) -> Self: | ||
first = next(iter(indexes)) | ||
if dim == "x": | ||
newx = PandasIndex.concat( | ||
tuple(i.x for i in indexes), dim=dim, positions=positions | ||
) | ||
newy = first.y | ||
elif dim == "y": | ||
newx = first.x | ||
newy = PandasIndex.concat( | ||
tuple(i.y for i in indexes), dim=dim, positions=positions | ||
) | ||
return cls(x=newx, y=newy) | ||
|
||
def isel(self, indexers: Mapping[Any, int | slice | np.ndarray | Variable]) -> Self: | ||
newx = self.x.isel({"x": indexers.get("x", slice(None))}) | ||
newy = self.y.isel({"y": indexers.get("y", slice(None))}) | ||
assert newx is not None | ||
assert newy is not None | ||
return type(self)(newx, newy) |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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 was wondering how this would behave for the corner case where the "y" coordinate (and/or the "x" coordinate) does not have any index in one or more of the objects to concatenate. We could leave it for now, though. It is quite unlikely that it will occur in practice I'd say.
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.
Not sure what you mean, we defer to the Index, so it can do what's sensible?