Skip to content

Commit b136fcb

Browse files
authored
Fix merge with compat=minimal (coord names) (#8104)
* fix coord names after merge / compat minimal * update what's new * add assert in len(data_vars)
1 parent e5a38f6 commit b136fcb

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Bug fixes
4343
- Improved handling of multi-coordinate indexes when updating coordinates, including bug fixes
4444
(and improved warnings for deprecated features) for pandas multi-indexes (:pull:`8094`).
4545
By `Benoît Bovy <https://github.com/benbovy>`_.
46+
- Fixed a bug in :py:func:`merge` with ``compat='minimal'`` where the coordinate
47+
names were not updated properly internally (:issue:`7405`, :issue:`7588`,
48+
:pull:`8104`).
49+
By `Benoît Bovy <https://github.com/benbovy>`_.
4650

4751
Documentation
4852
~~~~~~~~~~~~~

xarray/core/dataset.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@ def __iter__(self) -> Iterator[Hashable]:
438438
)
439439

440440
def __len__(self) -> int:
441-
return len(self._dataset._variables) - len(self._dataset._coord_names)
441+
length = len(self._dataset._variables) - len(self._dataset._coord_names)
442+
assert length >= 0, "something is wrong with Dataset._coord_names"
443+
return length
442444

443445
def __contains__(self, key: Hashable) -> bool:
444446
return key in self._dataset._variables and key not in self._dataset._coord_names

xarray/core/merge.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@ def merge_core(
723723
dims = calculate_dimensions(variables)
724724

725725
coord_names, noncoord_names = determine_coords(coerced)
726+
if compat == "minimal":
727+
# coordinates may be dropped in merged results
728+
coord_names.intersection_update(variables)
726729
if explicit_coords is not None:
727730
assert_valid_explicit_coords(variables, dims, explicit_coords)
728731
coord_names.update(explicit_coords)

xarray/tests/test_dataset.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,17 @@ def test_data_vars_properties(self) -> None:
10461046
"bar": np.dtype("float64"),
10471047
}
10481048

1049+
# len
1050+
ds.coords["x"] = [1]
1051+
assert len(ds.data_vars) == 2
1052+
1053+
# https://github.com/pydata/xarray/issues/7588
1054+
with pytest.raises(
1055+
AssertionError, match="something is wrong with Dataset._coord_names"
1056+
):
1057+
ds._coord_names = {"w", "x", "y", "z"}
1058+
len(ds.data_vars)
1059+
10491060
def test_equals_and_identical(self) -> None:
10501061
data = create_test_data(seed=42)
10511062
assert data.equals(data)

xarray/tests/test_merge.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ def test_merge_compat(self):
382382

383383
assert ds1.identical(ds1.merge(ds2, compat="override"))
384384

385+
def test_merge_compat_minimal(self) -> None:
386+
# https://github.com/pydata/xarray/issues/7405
387+
# https://github.com/pydata/xarray/issues/7588
388+
ds1 = xr.Dataset(coords={"foo": [1, 2, 3], "bar": 4})
389+
ds2 = xr.Dataset(coords={"foo": [1, 2, 3], "bar": 5})
390+
391+
actual = xr.merge([ds1, ds2], compat="minimal")
392+
expected = xr.Dataset(coords={"foo": [1, 2, 3]})
393+
assert_identical(actual, expected)
394+
385395
def test_merge_auto_align(self):
386396
ds1 = xr.Dataset({"a": ("x", [1, 2]), "x": [0, 1]})
387397
ds2 = xr.Dataset({"b": ("x", [3, 4]), "x": [1, 2]})

0 commit comments

Comments
 (0)