Skip to content

Commit

Permalink
Raises a ValueError for a confliction between dimension names and lev…
Browse files Browse the repository at this point in the history
…el names (#2353)

* Raises a ValueError for a confliction between dimension names and level names

* Clean up whatsnew.
  • Loading branch information
fujiisoup authored Aug 13, 2018
1 parent 846e28f commit e3350fd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ Bug fixes
attribute being set.
(:issue:`2201`)
By `Thomas Voigt <https://github.com/tv3141>`_.

- Tests can be run in parallel with pytest-xdist
- Follow up the renamings in dask; from dask.ghost to dask.overlap
By `Tony Tung <https://github.com/ttung>`_.

- Now raises a ValueError when there is a conflict between dimension names and
level names of MultiIndex. (:issue:`2299`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

- Follow up the renamings in dask; from dask.ghost to dask.overlap
By `Keisuke Fujii <https://github.com/fujiisoup>`_.

- Now :py:func:`xr.apply_ufunc` raises a ValueError when the size of
``input_core_dims`` is inconsistent with the number of arguments.
Expand Down
9 changes: 9 additions & 0 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,12 +1876,15 @@ def assert_unique_multiindex_level_names(variables):
objects.
"""
level_names = defaultdict(list)
all_level_names = set()
for var_name, var in variables.items():
if isinstance(var._data, PandasIndexAdapter):
idx_level_names = var.to_index_variable().level_names
if idx_level_names is not None:
for n in idx_level_names:
level_names[n].append('%r (%s)' % (n, var_name))
if idx_level_names:
all_level_names.update(idx_level_names)

for k, v in level_names.items():
if k in variables:
Expand All @@ -1892,3 +1895,9 @@ def assert_unique_multiindex_level_names(variables):
conflict_str = '\n'.join([', '.join(v) for v in duplicate_names])
raise ValueError('conflicting MultiIndex level name(s):\n%s'
% conflict_str)
# Check confliction between level names and dimensions GH:2299
for k, v in variables.items():
for d in v.dims:
if d in all_level_names:
raise ValueError('conflicting level / dimension names. {} '
'already exists as a level name.'.format(d))
12 changes: 12 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,18 @@ def test_assign_multiindex_level(self):
with raises_regex(ValueError, 'conflicting MultiIndex'):
data.assign(level_1=range(4))
data.assign_coords(level_1=range(4))
# raise an Error when any level name is used as dimension GH:2299
with pytest.raises(ValueError):
data['y'] = ('level_1', [0, 1])

def test_merge_multiindex_level(self):
data = create_test_multiindex()
other = Dataset({'z': ('level_1', [0, 1])}) # conflict dimension
with pytest.raises(ValueError):
data.merge(other)
other = Dataset({'level_1': ('x', [0, 1])}) # conflict variable name
with pytest.raises(ValueError):
data.merge(other)

def test_setitem_original_non_unique_index(self):
# regression test for GH943
Expand Down

0 comments on commit e3350fd

Please sign in to comment.