-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Coerce IndexVariable to Variable when assigning to data variables or coordinates
#10909
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
base: main
Are you sure you want to change the base?
Conversation
IndexVariable to Variable when assigning to data variables or coordinates
IndexVariable to Variable when assigning to data variables or coordinatesIndexVariable to Variable when assigning to data variables or coordinates
af095e7 to
3370345
Compare
shoyer
left a comment
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.
Looks great, thank you!
| for (k, v) in ds._variables.items() | ||
| if v.dims == (k,) | ||
| ), {k: type(v) for k, v in ds._variables.items() if v.dims == (k,)} | ||
|
|
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.
This is now handled by _assert_variable_invariants
|
|
||
| def _assert_variable_invariants(var: Variable, name: Hashable = None): | ||
| def _assert_variable_invariants( | ||
| var: Variable | Any, |
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.
The check now happens within this function.
| isinstance(v, IndexVariable) | ||
| for (k, v) in da._coords.items() | ||
| if v.dims == (k,) | ||
| ), {k: type(v) for k, v in da._coords.items()} |
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.
This is now handled by _assert_variable_invariants
| stacked.coords["variable"].drop_vars(["z", "variable", "y"]), | ||
| expected_stacked_variable, | ||
| stacked["variable"].variable.to_base_variable(), | ||
| expected_stacked_variable.variable, |
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.
This was the only test change that was needed. Basically stacked["variable"].variable is an IndexVariable because it is represents a coord that is part of a MultiIndex. But you can't know that if you only have access to the DataArray with no coords. So I just switched it to be comparing variable and to coerce to the Variable.
Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com>
whats-new.rstNew functions/methods are listed inapi.rst