-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Drop multi-indexes when assigning to a multi-indexed variable #6798
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 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3442d8f
Drop multi-indexes when assigning to a multi-indexed variable
dcherian 3a586b7
fix.
dcherian 44c94fa
Update xarray/core/coordinates.py
dcherian 23865b3
Add Data*Coordinates._drop_coords
dcherian 1b88ac4
Restore error when assigning to a MultiIndex level.
dcherian b3a32f1
fix
dcherian f1bdf8b
Update xarray/core/coordinates.py
dcherian e5762ec
Address review comments.
dcherian b69e851
Merge branch 'main' into fix-multiindex-propagate
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import warnings | ||
| from contextlib import contextmanager | ||
| from typing import TYPE_CHECKING, Any, Hashable, Iterator, Mapping, Sequence, cast | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
|
|
||
| from . import formatting | ||
| from .indexes import Index, Indexes, assert_no_index_corrupted | ||
| from .indexes import Index, Indexes, PandasMultiIndex, assert_no_index_corrupted | ||
| from .merge import merge_coordinates_without_align, merge_coords | ||
| from .utils import Frozen, ReprObject | ||
| from .variable import Variable, calculate_dimensions | ||
|
|
@@ -154,6 +155,7 @@ def to_index(self, ordered_dims: Sequence[Hashable] = None) -> pd.Index: | |
|
|
||
| def update(self, other: Mapping[Any, Any]) -> None: | ||
| other_vars = getattr(other, "variables", other) | ||
| self._drop_coords(other_vars) | ||
| coords, indexes = merge_coords( | ||
| [self.variables, other_vars], priority_arg=1, indexes=self.xindexes | ||
| ) | ||
|
|
@@ -304,6 +306,14 @@ def _update_coords( | |
| original_indexes.update(indexes) | ||
| self._data._indexes = original_indexes | ||
|
|
||
| def _drop_coords(self, coords: dict[Hashable, Variable]) -> None: | ||
| """Drops variables in coords, and any associated variables as well.""" | ||
| variables = self._data._variables.copy() | ||
| indexes = dict(self._data.xindexes) | ||
| drop_coords(coords, variables, indexes) | ||
| self._data._variables = variables | ||
| self._data._indexes = indexes | ||
|
|
||
| def __delitem__(self, key: Hashable) -> None: | ||
| if key in self: | ||
| del self._data[key] | ||
|
|
@@ -372,6 +382,14 @@ def _update_coords( | |
| original_indexes.update(indexes) | ||
| self._data._indexes = original_indexes | ||
|
|
||
| def _drop_coords(self, coords: dict[Hashable, Variable]) -> None: | ||
| """Drops variables in coords, and any associated variables as well.""" | ||
| variables = self._data._coords.copy() | ||
| indexes = dict(self._data.xindexes) | ||
| drop_coords(coords, variables, indexes) | ||
| self._data._coords = variables | ||
| self._data._indexes = indexes | ||
|
|
||
| @property | ||
| def variables(self): | ||
| return Frozen(self._data._coords) | ||
|
|
@@ -397,6 +415,25 @@ def _ipython_key_completions_(self): | |
| return self._data._ipython_key_completions_() | ||
|
|
||
|
|
||
| def drop_coords(coords_to_drop, variables, indexes): | ||
| """Drop index variables associated with variables in coords_to_drop.""" | ||
| for key in set(coords_to_drop) & set(indexes): | ||
| maybe_midx = indexes[key] | ||
| idx_coord_names = set(maybe_midx.index.names + [maybe_midx.dim]) | ||
dcherian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if isinstance(maybe_midx, PandasMultiIndex): | ||
| warnings.warn( | ||
| f"Updating MultiIndexed coordinate {key!r} would corrupt indices for " | ||
| f"other variables: {list(maybe_midx.index.names)!r}. " | ||
| f"This will raise an error in the future. Use `.drop_vars({idx_coord_names!r})` before " | ||
| "assigning new coordinate values.", | ||
| DeprecationWarning, | ||
| stacklevel=4, | ||
|
Contributor
Author
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. Should be 4 for |
||
| ) | ||
| for k in idx_coord_names: | ||
| del variables[k] | ||
| del indexes[k] | ||
|
|
||
|
|
||
| def assert_coordinate_consistent( | ||
| obj: DataArray | Dataset, coords: Mapping[Any, Variable] | ||
| ) -> None: | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.