-
-
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 4 commits
62e2f31
93281b7
3370345
7062b3b
6132204
93882c5
a6dde19
84c25d3
e530907
6440ab4
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 |
|---|---|---|
|
|
@@ -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
Contributor
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. yeah multi-indexes are where IndexVariable really bites us e.g. #8887 |
||
| ) | ||
|
|
||
| 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) | ||
jsignell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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.
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 agree with https://github.com/pydata/xarray/pull/10909/files#r2528970158, it may be better to keep the
IndexVariablevsVariablecheck at the level of Dataset and/or DataArray invariants.Actually, the right place should probably be
_assert_indexes_invariants_checks, where we could add the check that non-index variables are not instances ofIndexVariable.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 moved the
IndexVariablecheck, but I still think it is nicer to keep theVariablecheck within this function. Let me know if you'd still rather I move it out.