Skip to content

Commit 1f92e30

Browse files
committed
Remove warning
1 parent 5164d1e commit 1f92e30

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

xarray/core/variable.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,9 @@ def as_variable(obj, name=None) -> Variable | IndexVariable:
158158
f"explicit list of dimensions: {obj!r}"
159159
)
160160

161-
if name is not None and name in obj.dims:
162-
# convert the Variable into an Index
163-
if obj.ndim != 1:
164-
warnings.warn(
165-
f"{name!r} has more than 1-dimension and the same name as one of its "
166-
f"dimensions {obj.dims!r}. Xarray will not automatically "
167-
"create an Index object that would allow label-based selection.",
168-
RuntimeWarning,
169-
)
170-
else:
171-
obj = obj.to_index_variable()
161+
if name is not None and name in obj.dims and obj.ndim == 1:
162+
# automatically convert the Variable into an Index
163+
obj = obj.to_index_variable()
172164

173165
return obj
174166

xarray/tests/test_dataset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,9 @@ def test_constructor(self) -> None:
472472
with pytest.raises(ValueError, match=r"already exists as a scalar"):
473473
Dataset({"x": 0, "y": ("x", [1, 2, 3])})
474474

475-
with pytest.warns(RuntimeWarning, match=r"will not automatically"):
476-
actual = Dataset({"a": x1, "x": z})
477-
assert "x" not in actual.xindexes
475+
# nD coordinate variable "x" sharing name with dimension
476+
actual = Dataset({"a": x1, "x": z})
477+
assert "x" not in actual.xindexes
478478

479479
# verify handling of DataArrays
480480
expected = Dataset({"x": x1, "z": z})

xarray/tests/test_variable.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,9 +1247,10 @@ def test_as_variable(self):
12471247
expected = Variable(("x", "y"), data)
12481248
with pytest.raises(ValueError, match=r"without explicit dimension names"):
12491249
as_variable(data, name="x")
1250-
with pytest.warns(RuntimeWarning, match=r"has more than 1-dimension"):
1251-
actual = as_variable(expected, name="x")
1252-
assert_identical(expected, actual)
1250+
1251+
# name of nD variable matches dimension name
1252+
actual = as_variable(expected, name="x")
1253+
assert_identical(expected, actual)
12531254

12541255
# test datetime, timedelta conversion
12551256
dt = np.array([datetime(1999, 1, 1) + timedelta(days=x) for x in range(10)])

0 commit comments

Comments
 (0)