Skip to content
forked from pydata/xarray

Commit

Permalink
Warn instead of raising for nD index variable
Browse files Browse the repository at this point in the history
Avoid automatic creating of Index variable
when nD variable shares name with one
of its dimensions.

Closes pydata#2233
  • Loading branch information
dcherian committed Jul 15, 2023
1 parent 77e2db5 commit ec27da4
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,14 @@ def as_variable(obj, name=None) -> Variable | IndexVariable:
if name is not None and name in obj.dims:
# convert the Variable into an Index
if obj.ndim != 1:
raise MissingDimensionsError(
warnings.warn(
f"{name!r} has more than 1-dimension and the same name as one of its "
f"dimensions {obj.dims!r}. xarray disallows such variables because they "
"conflict with the coordinates used to label dimensions."
f"dimensions {obj.dims!r}. Xarray will not automatically"
"create an Index object that would allow label-based selection.",
RuntimeWarning,
)
obj = obj.to_index_variable()
else:
obj = obj.to_index_variable()

return obj

Expand Down

0 comments on commit ec27da4

Please sign in to comment.