Skip to content

Commit e3350fd

Browse files
authored
Raises a ValueError for a confliction between dimension names and level names (#2353)
* Raises a ValueError for a confliction between dimension names and level names * Clean up whatsnew.
1 parent 846e28f commit e3350fd

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

doc/whats-new.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,16 @@ Bug fixes
6161
attribute being set.
6262
(:issue:`2201`)
6363
By `Thomas Voigt <https://github.com/tv3141>`_.
64+
6465
- Tests can be run in parallel with pytest-xdist
65-
- Follow up the renamings in dask; from dask.ghost to dask.overlap
66+
By `Tony Tung <https://github.com/ttung>`_.
67+
68+
- Now raises a ValueError when there is a conflict between dimension names and
69+
level names of MultiIndex. (:issue:`2299`)
6670
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
6771

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

6975
- Now :py:func:`xr.apply_ufunc` raises a ValueError when the size of
7076
``input_core_dims`` is inconsistent with the number of arguments.

xarray/core/variable.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,12 +1876,15 @@ def assert_unique_multiindex_level_names(variables):
18761876
objects.
18771877
"""
18781878
level_names = defaultdict(list)
1879+
all_level_names = set()
18791880
for var_name, var in variables.items():
18801881
if isinstance(var._data, PandasIndexAdapter):
18811882
idx_level_names = var.to_index_variable().level_names
18821883
if idx_level_names is not None:
18831884
for n in idx_level_names:
18841885
level_names[n].append('%r (%s)' % (n, var_name))
1886+
if idx_level_names:
1887+
all_level_names.update(idx_level_names)
18851888

18861889
for k, v in level_names.items():
18871890
if k in variables:
@@ -1892,3 +1895,9 @@ def assert_unique_multiindex_level_names(variables):
18921895
conflict_str = '\n'.join([', '.join(v) for v in duplicate_names])
18931896
raise ValueError('conflicting MultiIndex level name(s):\n%s'
18941897
% conflict_str)
1898+
# Check confliction between level names and dimensions GH:2299
1899+
for k, v in variables.items():
1900+
for d in v.dims:
1901+
if d in all_level_names:
1902+
raise ValueError('conflicting level / dimension names. {} '
1903+
'already exists as a level name.'.format(d))

xarray/tests/test_dataset.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,6 +2456,18 @@ def test_assign_multiindex_level(self):
24562456
with raises_regex(ValueError, 'conflicting MultiIndex'):
24572457
data.assign(level_1=range(4))
24582458
data.assign_coords(level_1=range(4))
2459+
# raise an Error when any level name is used as dimension GH:2299
2460+
with pytest.raises(ValueError):
2461+
data['y'] = ('level_1', [0, 1])
2462+
2463+
def test_merge_multiindex_level(self):
2464+
data = create_test_multiindex()
2465+
other = Dataset({'z': ('level_1', [0, 1])}) # conflict dimension
2466+
with pytest.raises(ValueError):
2467+
data.merge(other)
2468+
other = Dataset({'level_1': ('x', [0, 1])}) # conflict variable name
2469+
with pytest.raises(ValueError):
2470+
data.merge(other)
24592471

24602472
def test_setitem_original_non_unique_index(self):
24612473
# regression test for GH943

0 commit comments

Comments
 (0)