Skip to content

Commit 2120808

Browse files
authored
Allow multidimensional variable with same name as dim when constructing dataset via coords (#8886)
* regression test * remove unnecessary check * whatsnew * remove outdated test
1 parent cf36559 commit 2120808

File tree

4 files changed

+12
-29
lines changed

4 files changed

+12
-29
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ Bug fixes
7373
- Warn and return bytes undecoded in case of UnicodeDecodeError in h5netcdf-backend
7474
(:issue:`5563`, :pull:`8874`).
7575
By `Kai Mühlbauer <https://github.com/kmuehlbauer>`_.
76+
- Fix bug incorrectly disallowing creation of a dataset with a multidimensional coordinate variable with the same name as one of its dims.
77+
(:issue:`8884`, :pull:`8886`)
78+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
7679

7780

7881
Documentation

xarray/core/merge.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -562,25 +562,6 @@ def merge_coords(
562562
return variables, out_indexes
563563

564564

565-
def assert_valid_explicit_coords(
566-
variables: Mapping[Any, Any],
567-
dims: Mapping[Any, int],
568-
explicit_coords: Iterable[Hashable],
569-
) -> None:
570-
"""Validate explicit coordinate names/dims.
571-
572-
Raise a MergeError if an explicit coord shares a name with a dimension
573-
but is comprised of arbitrary dimensions.
574-
"""
575-
for coord_name in explicit_coords:
576-
if coord_name in dims and variables[coord_name].dims != (coord_name,):
577-
raise MergeError(
578-
f"coordinate {coord_name} shares a name with a dataset dimension, but is "
579-
"not a 1D variable along that dimension. This is disallowed "
580-
"by the xarray data model."
581-
)
582-
583-
584565
def merge_attrs(variable_attrs, combine_attrs, context=None):
585566
"""Combine attributes from different variables according to combine_attrs"""
586567
if not variable_attrs:
@@ -728,7 +709,6 @@ def merge_core(
728709
# coordinates may be dropped in merged results
729710
coord_names.intersection_update(variables)
730711
if explicit_coords is not None:
731-
assert_valid_explicit_coords(variables, dims, explicit_coords)
732712
coord_names.update(explicit_coords)
733713
for dim, size in dims.items():
734714
if dim in variables:

xarray/tests/test_coordinates.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import numpy as np
34
import pandas as pd
45
import pytest
56

@@ -8,7 +9,7 @@
89
from xarray.core.dataarray import DataArray
910
from xarray.core.dataset import Dataset
1011
from xarray.core.indexes import PandasIndex, PandasMultiIndex
11-
from xarray.core.variable import IndexVariable
12+
from xarray.core.variable import IndexVariable, Variable
1213
from xarray.tests import assert_identical, source_ndarray
1314

1415

@@ -174,3 +175,10 @@ def test_align(self) -> None:
174175
left2, right2 = align(left, right, join="override")
175176
assert_identical(left2, left)
176177
assert_identical(left2, right2)
178+
179+
def test_dataset_from_coords_with_multidim_var_same_name(self):
180+
# regression test for GH #8883
181+
var = Variable(data=np.arange(6).reshape(2, 3), dims=["x", "y"])
182+
coords = Coordinates(coords={"x": var}, indexes={})
183+
ds = Dataset(coords=coords)
184+
assert ds.coords["x"].dims == ("x", "y")

xarray/tests/test_dataset.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,6 @@ def test_constructor(self) -> None:
486486
actual = Dataset({"z": expected["z"]})
487487
assert_identical(expected, actual)
488488

489-
def test_constructor_invalid_dims(self) -> None:
490-
# regression for GH1120
491-
with pytest.raises(MergeError):
492-
Dataset(
493-
data_vars=dict(v=("y", [1, 2, 3, 4])),
494-
coords=dict(y=DataArray([0.1, 0.2, 0.3, 0.4], dims="x")),
495-
)
496-
497489
def test_constructor_1d(self) -> None:
498490
expected = Dataset({"x": (["x"], 5.0 + np.arange(5))})
499491
actual = Dataset({"x": 5.0 + np.arange(5)})

0 commit comments

Comments
 (0)