-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
modification methods on Coordinates
#10318
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
Open
keewis
wants to merge
15
commits into
pydata:main
Choose a base branch
from
keewis:coords-methods
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−2
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
eb6dc62
implement `Coordinates` methods modifying coords
keewis 8a18a52
allow using the binary-or operator (`|`) for merging
keewis f8b7f57
tests for `drop_vars`
keewis 92e1e8a
Merge branch 'main' into coords-methods
keewis 4626ce5
tests for `rename_dims` and `rename_vars`
keewis a370ed9
tests for the merge operator
keewis c909ca9
Apply suggestions from code review
keewis 836aeca
Merge branch 'main' into coords-methods
keewis 22c89e3
attempt to fix the typing
keewis 3b9e3e5
make sure we always return a `Coordinates` object
keewis e53b848
replace docstring by a reference to `Coordinates.merge`
keewis b204459
Merge branch 'main' into coords-methods
keewis 7dbd26a
changelog
keewis 059512a
create docs pages
keewis 773f149
add back the docstring for `__or__`
keewis 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
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Hashable, Iterator, Mapping, Sequence | ||
from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping, Sequence | ||
from contextlib import contextmanager | ||
from typing import ( | ||
TYPE_CHECKING, | ||
|
@@ -21,7 +21,7 @@ | |
assert_no_index_corrupted, | ||
create_default_index_implicit, | ||
) | ||
from xarray.core.types import DataVars, Self, T_DataArray, T_Xarray | ||
from xarray.core.types import DataVars, ErrorOptions, Self, T_DataArray, T_Xarray | ||
from xarray.core.utils import ( | ||
Frozen, | ||
ReprObject, | ||
|
@@ -561,6 +561,35 @@ def merge(self, other: Mapping[Any, Any] | None) -> Dataset: | |
variables=coords, coord_names=coord_names, indexes=indexes | ||
) | ||
|
||
def __or__(self, other: Mapping[Any, Any] | None) -> Coordinates: | ||
"""Merge two sets of coordinates to create a new Coordinates object | ||
|
||
The method implements the logic used for joining coordinates in the | ||
result of a binary operation performed on xarray objects: | ||
|
||
- If two index coordinates conflict (are not equal), an exception is | ||
raised. You must align your data before passing it to this method. | ||
- If an index coordinate and a non-index coordinate conflict, the non- | ||
index coordinate is dropped. | ||
- If two non-index coordinates conflict, both are dropped. | ||
|
||
Parameters | ||
---------- | ||
other : dict-like, optional | ||
A :py:class:`Coordinates` object or any mapping that can be turned | ||
into coordinates. | ||
|
||
Returns | ||
------- | ||
merged : Coordinates | ||
A new Coordinates object with merged coordinates. | ||
|
||
See Also | ||
-------- | ||
Coordinates.merge | ||
""" | ||
return self.merge(other).coords | ||
|
||
def __setitem__(self, key: Hashable, value: Any) -> None: | ||
self.update({key: value}) | ||
|
||
|
@@ -719,6 +748,83 @@ def copy( | |
), | ||
) | ||
|
||
def drop_vars( | ||
self, | ||
names: str | ||
| Iterable[Hashable] | ||
| Callable[ | ||
[Coordinates | Dataset | DataArray | DataTree], | ||
str | Iterable[Hashable], | ||
], | ||
*, | ||
errors: ErrorOptions = "raise", | ||
) -> Self: | ||
"""Drop variables from this Coordinates object. | ||
|
||
Note that indexes that depend on these variables will also be dropped. | ||
|
||
Parameters | ||
---------- | ||
names : hashable or iterable or callable | ||
Name(s) of variables to drop. If a callable, this is object is passed as its | ||
only argument and its result is used. | ||
errors : {"raise", "ignore"}, default: "raise" | ||
Error treatment. | ||
|
||
- ``'raise'``: raises a :py:class:`ValueError` error if any of the variable | ||
passed are not in the dataset | ||
- ``'ignore'``: any given names that are in the dataset are dropped and no | ||
error is raised. | ||
""" | ||
return cast(Self, self.to_dataset().drop_vars(names, errors=errors).coords) | ||
|
||
def rename_dims( | ||
self, | ||
dims_dict: Mapping[Any, Hashable] | None = None, | ||
**dims: Hashable, | ||
) -> Coordinates: | ||
"""Returns a new object with renamed dimensions only. | ||
|
||
Parameters | ||
---------- | ||
dims_dict : dict-like, optional | ||
Dictionary whose keys are current dimension names and | ||
whose values are the desired names. The desired names must | ||
not be the name of an existing dimension or Variable in the Coordinates. | ||
**dims : optional | ||
Keyword form of ``dims_dict``. | ||
One of dims_dict or dims must be provided. | ||
|
||
Returns | ||
------- | ||
renamed : Coordinates | ||
Coordinates object with renamed dimensions. | ||
""" | ||
return self.to_dataset().rename_dims(dims_dict, **dims).coords | ||
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.
|
||
|
||
def rename_vars( | ||
self, | ||
name_dict: Mapping[Any, Hashable] | None = None, | ||
**names: Hashable, | ||
) -> Coordinates: | ||
"""Returns a new object with renamed variables. | ||
|
||
Parameters | ||
---------- | ||
name_dict : dict-like, optional | ||
Dictionary whose keys are current variable or coordinate names and | ||
whose values are the desired names. | ||
**names : optional | ||
Keyword form of ``name_dict``. | ||
One of name_dict or names must be provided. | ||
|
||
Returns | ||
------- | ||
renamed : Coordinates | ||
Coordinates object with renamed variables | ||
""" | ||
return self.to_dataset().rename_vars(name_dict, **names).coords | ||
|
||
|
||
class DatasetCoordinates(Coordinates): | ||
"""Dictionary like container for Dataset coordinates (variables + indexes). | ||
|
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.
Do we need such docstrings for an operator?
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 really, but it serves as a comment in this case. No need to copy everything, though, so we can remove everything but the first line and add a
See Also
section toCoordinates.merge
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.
It looks like we're generating documentation pages for
__setitem__
and__getitem__
. Not sure if this actually makes sense, but following that I added a page for__or__
so we actually do need the full docstring.