Skip to content

Commit 45cdb80

Browse files
author
Benoit Bovy
committed
fix adding coords/vars with the same name than a multi-index level
1 parent 9dc2c16 commit 45cdb80

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

xarray/core/coordinates.py

+12
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ def to_dataset(self):
151151
def _update_coords(self, coords):
152152
from .dataset import calculate_dimensions
153153

154+
for key in coords:
155+
if key in self._data._level_coords:
156+
raise ValueError("%r is already a MultiIndex level of "
157+
"coordinate %r"
158+
% (key, self._data._level_coords[key]))
159+
154160
variables = self._data._variables.copy()
155161
variables.update(coords)
156162

@@ -197,6 +203,12 @@ def _names(self):
197203
def _update_coords(self, coords):
198204
from .dataset import calculate_dimensions
199205

206+
for key in coords:
207+
if key in self._data._level_coords:
208+
raise ValueError("%r is already a MultiIndex level of "
209+
"coordinate %r"
210+
% (key, self._data._level_coords[key]))
211+
200212
dims = calculate_dimensions(coords)
201213
if set(dims) != set(self.dims):
202214
raise ValueError('cannot add coordinates with new dimensions to '

xarray/core/dataarray.py

-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ def _infer_coords_and_dims(shape, coords, dims):
6969
new_coords[dim] = default_index_coordinate(dim, size)
7070

7171
sizes = dict(zip(dims, shape))
72-
level_names = {}
7372
for k, v in new_coords.items():
7473
if any(d not in dims for d in v.dims):
7574
raise ValueError('coordinate %s has dimensions %s, but these '

xarray/core/dataset.py

+6
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,12 @@ def __setitem__(self, key, value):
565565
if utils.is_dict_like(key):
566566
raise NotImplementedError('cannot yet use a dictionary as a key '
567567
'to set Dataset values')
568+
569+
if key in self._level_coords:
570+
raise ValueError("%r is already a MultiIndex level of "
571+
"coordinate %r"
572+
% (key, self._level_coords[key]))
573+
568574
self.update({key: value})
569575

570576
def __delitem__(self, key):

0 commit comments

Comments
 (0)