Skip to content

Commit 8938d39

Browse files
authored
Fix assign_coords resetting all dimension coords to default index (#7347)
* fix merge_coords and collect_variables_and_indexes * add test * update what's new * update what's new with pull-request link
1 parent 92e7cb5 commit 8938d39

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ Bug fixes
6565
By `Michael Niklas <https://github.com/headtr1ck>`_.
6666
- Fix multiple reads on fsspec S3 files by resetting file pointer to 0 when reading file streams (:issue:`6813`, :pull:`7304`).
6767
By `David Hoese <https://github.com/djhoese>`_ and `Wei Ji Leong <https://github.com/weiji14>`_.
68+
- Fix :py:meth:`Dataset.assign_coords` resetting all dimension coordinates to default (pandas) index (:issue:`7346`, :pull:`7347`).
69+
By `Benoît Bovy <https://github.com/benbovy>`_.
6870

6971
Documentation
7072
~~~~~~~~~~~~~

xarray/core/merge.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,12 @@ def append_all(variables, indexes):
355355

356356
for name, variable in mapping.items():
357357
if isinstance(variable, DataArray):
358-
coords = variable._coords.copy() # use private API for speed
359-
indexes = dict(variable._indexes)
358+
coords_ = variable._coords.copy() # use private API for speed
359+
indexes_ = dict(variable._indexes)
360360
# explicitly overwritten variables should take precedence
361-
coords.pop(name, None)
362-
indexes.pop(name, None)
363-
append_all(coords, indexes)
361+
coords_.pop(name, None)
362+
indexes_.pop(name, None)
363+
append_all(coords_, indexes_)
364364

365365
variable = as_variable(variable, name=name)
366366
if name in indexes:
@@ -561,7 +561,7 @@ def merge_coords(
561561
aligned = deep_align(
562562
coerced, join=join, copy=False, indexes=indexes, fill_value=fill_value
563563
)
564-
collected = collect_variables_and_indexes(aligned)
564+
collected = collect_variables_and_indexes(aligned, indexes=indexes)
565565
prioritized = _get_priority_vars_and_indexes(aligned, priority_arg, compat=compat)
566566
variables, out_indexes = merge_collected(collected, prioritized, compat=compat)
567567
return variables, out_indexes

xarray/tests/test_dataset.py

+14
Original file line numberDiff line numberDiff line change
@@ -4170,6 +4170,20 @@ def test_assign_all_multiindex_coords(self) -> None:
41704170
is not actual.xindexes["level_2"]
41714171
)
41724172

4173+
def test_assign_coords_custom_index_side_effect(self) -> None:
4174+
# test that assigning new coordinates do not reset other dimension coord indexes
4175+
# to default (pandas) index (https://github.com/pydata/xarray/issues/7346)
4176+
class CustomIndex(PandasIndex):
4177+
pass
4178+
4179+
ds = (
4180+
Dataset(coords={"x": [1, 2, 3]})
4181+
.drop_indexes("x")
4182+
.set_xindex("x", CustomIndex)
4183+
)
4184+
actual = ds.assign_coords(y=[4, 5, 6])
4185+
assert isinstance(actual.xindexes["x"], CustomIndex)
4186+
41734187
def test_merge_multiindex_level(self) -> None:
41744188
data = create_test_multiindex()
41754189

0 commit comments

Comments
 (0)