-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import functools | ||
| import warnings | ||
| from collections.abc import Hashable | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
|
|
@@ -401,11 +402,25 @@ def _assert_indexes_invariants_checks( | |
| ) | ||
|
|
||
|
|
||
| def _assert_variable_invariants(var: Variable, name: Hashable = None): | ||
| def _assert_variable_invariants( | ||
| var: Variable | Any, | ||
| name: Hashable = None, | ||
| check_default_indexes: bool = True, | ||
| is_index: bool = False, | ||
| ) -> None: | ||
| if name is None: | ||
| name_or_empty: tuple = () | ||
| else: | ||
| name_or_empty = (name,) | ||
|
|
||
| assert isinstance(var, Variable), {name: type(var)} | ||
| if name: | ||
| if var.dims == name_or_empty: | ||
| if check_default_indexes: | ||
| assert isinstance(var, IndexVariable), {name: type(var)} | ||
| elif not is_index: | ||
| assert not isinstance(var, IndexVariable), {name: type(var)} | ||
|
|
||
| assert isinstance(var._dims, tuple), name_or_empty + (var._dims,) | ||
| assert len(var._dims) == len(var._data.shape), name_or_empty + ( | ||
| var._dims, | ||
|
|
@@ -418,25 +433,20 @@ def _assert_variable_invariants(var: Variable, name: Hashable = None): | |
|
|
||
|
|
||
| def _assert_dataarray_invariants(da: DataArray, check_default_indexes: bool): | ||
| assert isinstance(da._variable, Variable), da._variable | ||
| _assert_variable_invariants(da._variable) | ||
| _assert_variable_invariants(da._variable, name=da.name, check_default_indexes=False) | ||
|
|
||
| assert isinstance(da._coords, dict), da._coords | ||
| assert all(isinstance(v, Variable) for v in da._coords.values()), da._coords | ||
|
|
||
| if check_default_indexes: | ||
| assert all(set(v.dims) <= set(da.dims) for v in da._coords.values()), ( | ||
| da.dims, | ||
| {k: v.dims for k, v in da._coords.items()}, | ||
| ) | ||
| assert all( | ||
| isinstance(v, IndexVariable) | ||
| for (k, v) in da._coords.items() | ||
| if v.dims == (k,) | ||
| ), {k: type(v) for k, v in da._coords.items()} | ||
|
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. This is now handled by |
||
|
|
||
| for k, v in da._coords.items(): | ||
| _assert_variable_invariants(v, k) | ||
| _assert_variable_invariants( | ||
| v, k, check_default_indexes=check_default_indexes, is_index=k in da._indexes | ||
| ) | ||
|
|
||
| if da._indexes is not None: | ||
| _assert_indexes_invariants_checks( | ||
|
|
@@ -446,9 +456,11 @@ def _assert_dataarray_invariants(da: DataArray, check_default_indexes: bool): | |
|
|
||
| def _assert_dataset_invariants(ds: Dataset, check_default_indexes: bool): | ||
| assert isinstance(ds._variables, dict), type(ds._variables) | ||
| assert all(isinstance(v, Variable) for v in ds._variables.values()), ds._variables | ||
|
|
||
| for k, v in ds._variables.items(): | ||
| _assert_variable_invariants(v, k) | ||
| _assert_variable_invariants( | ||
| v, k, check_default_indexes=check_default_indexes, is_index=k in ds._indexes | ||
| ) | ||
|
|
||
| assert isinstance(ds._coord_names, set), ds._coord_names | ||
| assert ds._coord_names <= ds._variables.keys(), ( | ||
|
|
@@ -466,13 +478,6 @@ def _assert_dataset_invariants(ds: Dataset, check_default_indexes: bool): | |
| ds._dims[k] == v.sizes[k] for v in ds._variables.values() for k in v.sizes | ||
| ), (ds._dims, {k: v.sizes for k, v in ds._variables.items()}) | ||
|
|
||
| if check_default_indexes: | ||
| assert all( | ||
| isinstance(v, IndexVariable) | ||
| 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,)} | ||
|
|
||
|
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. This is now handled by |
||
| if ds._indexes is not None: | ||
| _assert_indexes_invariants_checks( | ||
| ds._indexes, ds._variables, ds._dims, check_default=check_default_indexes | ||
|
|
@@ -492,7 +497,7 @@ def _assert_internal_invariants( | |
| private APIs. | ||
| """ | ||
| if isinstance(xarray_obj, Variable): | ||
| _assert_variable_invariants(xarray_obj) | ||
| _assert_variable_invariants(xarray_obj, check_default_indexes=False) | ||
| elif isinstance(xarray_obj, DataArray): | ||
| _assert_dataarray_invariants( | ||
| xarray_obj, check_default_indexes=check_default_indexes | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4311,9 +4311,11 @@ def test_to_stacked_array_preserves_dtype(self) -> None: | |
| # coordinate created from variables names should be of string dtype | ||
| data = np.array(["a", "a", "a", "b"], dtype="<U1") | ||
| expected_stacked_variable = DataArray(name="variable", data=data, dims="z") | ||
|
|
||
| # coerce from `IndexVariable` to `Variable` before comparing | ||
| assert_identical( | ||
| stacked.coords["variable"].drop_vars(["z", "variable", "y"]), | ||
| expected_stacked_variable, | ||
| stacked["variable"].variable.to_base_variable(), | ||
| expected_stacked_variable.variable, | ||
|
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. This was the only test change that was needed. Basically |
||
| ) | ||
|
|
||
| def test_to_stacked_array_transposed(self) -> None: | ||
|
|
@@ -4779,6 +4781,11 @@ def test_setitem_using_list_errors(self, var_list, data, error_regex) -> None: | |
| with pytest.raises(ValueError, match=error_regex): | ||
| actual[var_list] = data | ||
|
|
||
| def test_setitem_uses_base_variable_class_even_for_index_variables(self) -> None: | ||
| ds = Dataset(coords={"x": [1, 2, 3]}) | ||
| ds["y"] = ds["x"] | ||
| _assert_internal_invariants(ds, check_default_indexes=True) | ||
|
|
||
| def test_assign(self) -> None: | ||
| ds = Dataset() | ||
| actual = ds.assign(x=[0, 1, 2], y=2) | ||
|
|
||
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.