Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raises a ValueError for a confliction between dimension names and level names #2353

Merged
merged 3 commits into from
Aug 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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