From aae007736a68462299e36e66481ba589b1fbe758 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Thu, 28 Mar 2024 10:28:41 -0400 Subject: [PATCH 1/4] regression test --- xarray/tests/test_coordinates.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/xarray/tests/test_coordinates.py b/xarray/tests/test_coordinates.py index 40743194ce6..f88e554d333 100644 --- a/xarray/tests/test_coordinates.py +++ b/xarray/tests/test_coordinates.py @@ -1,5 +1,6 @@ from __future__ import annotations +import numpy as np import pandas as pd import pytest @@ -8,7 +9,7 @@ from xarray.core.dataarray import DataArray from xarray.core.dataset import Dataset from xarray.core.indexes import PandasIndex, PandasMultiIndex -from xarray.core.variable import IndexVariable +from xarray.core.variable import IndexVariable, Variable from xarray.tests import assert_identical, source_ndarray @@ -174,3 +175,10 @@ def test_align(self) -> None: left2, right2 = align(left, right, join="override") assert_identical(left2, left) assert_identical(left2, right2) + + def test_dataset_from_coords_with_multidim_var_same_name(self): + # regression test for GH #8883 + var = Variable(data=np.arange(6).reshape(2, 3), dims=["x", "y"]) + coords = Coordinates(coords={"x": var}, indexes={}) + ds = Dataset(coords=coords) + assert ds.coords["x"].dims == ("x", "y") From 97638feba15eb40d5407047ebca691ddfc2472f0 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Thu, 28 Mar 2024 10:29:03 -0400 Subject: [PATCH 2/4] remove unnecessary check --- xarray/core/merge.py | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/xarray/core/merge.py b/xarray/core/merge.py index cbd06c8fdc5..a90e59e7c0b 100644 --- a/xarray/core/merge.py +++ b/xarray/core/merge.py @@ -562,25 +562,6 @@ def merge_coords( return variables, out_indexes -def assert_valid_explicit_coords( - variables: Mapping[Any, Any], - dims: Mapping[Any, int], - explicit_coords: Iterable[Hashable], -) -> None: - """Validate explicit coordinate names/dims. - - Raise a MergeError if an explicit coord shares a name with a dimension - but is comprised of arbitrary dimensions. - """ - for coord_name in explicit_coords: - if coord_name in dims and variables[coord_name].dims != (coord_name,): - raise MergeError( - f"coordinate {coord_name} shares a name with a dataset dimension, but is " - "not a 1D variable along that dimension. This is disallowed " - "by the xarray data model." - ) - - def merge_attrs(variable_attrs, combine_attrs, context=None): """Combine attributes from different variables according to combine_attrs""" if not variable_attrs: @@ -728,7 +709,6 @@ def merge_core( # coordinates may be dropped in merged results coord_names.intersection_update(variables) if explicit_coords is not None: - assert_valid_explicit_coords(variables, dims, explicit_coords) coord_names.update(explicit_coords) for dim, size in dims.items(): if dim in variables: From c7d8d6dc119ef4d8565b9f8b61f7d91f64807609 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Thu, 28 Mar 2024 10:42:35 -0400 Subject: [PATCH 3/4] whatsnew --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e8ce0cfffba..e3581876e59 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -73,6 +73,9 @@ Bug fixes - Warn and return bytes undecoded in case of UnicodeDecodeError in h5netcdf-backend (:issue:`5563`, :pull:`8874`). By `Kai Mühlbauer `_. +- Fix bug incorrectly disallowing creation of a dataset with a multidimensional coordinate variable with the same name as one of its dims. + (:issue:`8884`, :pull:`8886`) + By `Tom Nicholas `_. Documentation From 0df7cac47e2d5504991f7254a2969bd66d7434a7 Mon Sep 17 00:00:00 2001 From: TomNicholas Date: Thu, 28 Mar 2024 12:02:56 -0400 Subject: [PATCH 4/4] remove outdated test --- xarray/tests/test_dataset.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 39c404d096b..e2a64964775 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -486,14 +486,6 @@ def test_constructor(self) -> None: actual = Dataset({"z": expected["z"]}) assert_identical(expected, actual) - def test_constructor_invalid_dims(self) -> None: - # regression for GH1120 - with pytest.raises(MergeError): - Dataset( - data_vars=dict(v=("y", [1, 2, 3, 4])), - coords=dict(y=DataArray([0.1, 0.2, 0.3, 0.4], dims="x")), - ) - def test_constructor_1d(self) -> None: expected = Dataset({"x": (["x"], 5.0 + np.arange(5))}) actual = Dataset({"x": 5.0 + np.arange(5)})