Skip to content

Commit

Permalink
Allow multidimensional variable with same name as dim when constructi…
Browse files Browse the repository at this point in the history
…ng dataset via coords (#8886)

* regression test

* remove unnecessary check

* whatsnew

* remove outdated test
  • Loading branch information
TomNicholas authored Mar 28, 2024
1 parent cf36559 commit 2120808
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 29 deletions.
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/kmuehlbauer>`_.
- 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 <https://github.com/TomNicholas>`_.


Documentation
Expand Down
20 changes: 0 additions & 20 deletions xarray/core/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion xarray/tests/test_coordinates.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import numpy as np
import pandas as pd
import pytest

Expand All @@ -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


Expand Down Expand Up @@ -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")
8 changes: 0 additions & 8 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand Down

0 comments on commit 2120808

Please sign in to comment.