Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Bug Fixes
- Fix error handling issue in ``decode_cf_variables`` when decoding fails - the exception is now re-raised
correctly, with a note added about the variable name that caused the error (:issue:`10873`, :pull:`10886`).
By `Jonas L. Bertelsen <https://github.com/jonaslb>`_
- When assigning an indexed coordinate to a data variable or coordinate, coerce it from
``IndexVariable`` to ``Variable`` (:issue:`9859`, :issue:`10829`, :pull:`10909`)
By `Julia Signell <https://github.com/jsignell>`_

Performance
~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ def create_coords_with_default_indexes(
variables.update(idx_vars)
all_variables.update(idx_vars)
else:
variables[name] = variable
variables[name] = variable.to_base_variable()

new_coords = Coordinates._construct_direct(coords=variables, indexes=indexes)

Expand Down
13 changes: 11 additions & 2 deletions xarray/structure/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
emit_user_level_warning,
equivalent,
)
from xarray.core.variable import Variable, as_variable, calculate_dimensions
from xarray.core.variable import (
IndexVariable,
Variable,
as_variable,
calculate_dimensions,
)
from xarray.structure.alignment import deep_align
from xarray.util.deprecation_helpers import (
_COMPAT_DEFAULT,
Expand Down Expand Up @@ -1206,7 +1211,11 @@ def dataset_update_method(dataset: Dataset, other: CoercibleMapping) -> _MergeRe
if c not in value.dims and c in dataset.coords
]
if coord_names:
other[key] = value.drop_vars(coord_names)
value = value.drop_vars(coord_names)
if isinstance(value.variable, IndexVariable):
variable = value.variable.to_base_variable()
value = value._replace(variable=variable)
other[key] = value

return merge_core(
[dataset, other],
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1702,6 +1702,11 @@ def should_add_coord_to_array(self, name, var, dims):
assert_identical(actual, expected, check_default_indexes=False)
assert "x_bnds" not in actual.dims

def test_assign_coords_uses_base_variable_class(self) -> None:
a = DataArray([0, 1, 2], dims=["x"], coords={"x": [0, 1, 2]})
a = a.assign_coords(foo=a.x)
assert not isinstance(a["foo"].variable, IndexVariable)

def test_coords_alignment(self) -> None:
lhs = DataArray([1, 2, 3], [("x", [0, 1, 2])])
rhs = DataArray([2, 3, 4], [("x", [1, 2, 3])])
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4779,6 +4779,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 not isinstance(ds["y"].variable, IndexVariable)

def test_assign(self) -> None:
ds = Dataset()
actual = ds.assign(x=[0, 1, 2], y=2)
Expand Down
Loading