Skip to content

Commit ec27da4

Browse files
committed
Warn instead of raising for nD index variable
Avoid automatic creating of Index variable when nD variable shares name with one of its dimensions. Closes pydata#2233
1 parent 77e2db5 commit ec27da4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

xarray/core/variable.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,14 @@ def as_variable(obj, name=None) -> Variable | IndexVariable:
161161
if name is not None and name in obj.dims:
162162
# convert the Variable into an Index
163163
if obj.ndim != 1:
164-
raise MissingDimensionsError(
164+
warnings.warn(
165165
f"{name!r} has more than 1-dimension and the same name as one of its "
166-
f"dimensions {obj.dims!r}. xarray disallows such variables because they "
167-
"conflict with the coordinates used to label dimensions."
166+
f"dimensions {obj.dims!r}. Xarray will not automatically"
167+
"create an Index object that would allow label-based selection.",
168+
RuntimeWarning,
168169
)
169-
obj = obj.to_index_variable()
170+
else:
171+
obj = obj.to_index_variable()
170172

171173
return obj
172174

0 commit comments

Comments
 (0)