From 1f0f78158cbd8095d34509ba69c817030cc30510 Mon Sep 17 00:00:00 2001 From: fujiisoup Date: Wed, 8 Aug 2018 09:47:40 +0900 Subject: [PATCH 1/2] Raises a ValueError for a confliction between dimension names and level names --- doc/whats-new.rst | 5 ++++- xarray/core/variable.py | 9 +++++++++ xarray/tests/test_dataset.py | 12 ++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 113585837c3..f3336e31128 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -56,12 +56,15 @@ Enhancements Bug fixes ~~~~~~~~~ -- Fixed ``DataArray.to_iris()`` failure while creating ``DimCoord`` by +- Fixed ``DataArray.to_iris()`` failure while creating ``DimCoord`` by falling back to creating ``AuxCoord``. Fixed dependency on ``var_name`` attribute being set. (:issue:`2201`) By `Thomas Voigt `_. - Tests can be run in parallel with pytest-xdist +- Now raises a ValueError when there is a conflict between dimension names and + level names of MultiIndex. (:issue:`2299`) + By `Keisuke Fujii `_. .. _whats-new.0.10.8: diff --git a/xarray/core/variable.py b/xarray/core/variable.py index 8b6a3d3bf21..d9772407b82 100644 --- a/xarray/core/variable.py +++ b/xarray/core/variable.py @@ -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: @@ -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)) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index c0516ed7e56..08d71d462d8 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -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 From 82475aff193036c4b1493081414fc66befbfc150 Mon Sep 17 00:00:00 2001 From: fujiisoup Date: Tue, 14 Aug 2018 07:13:12 +0900 Subject: [PATCH 2/2] Clean up whatsnew. --- doc/whats-new.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 19310d2ca1e..0c62cca6093 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -61,12 +61,16 @@ Bug fixes attribute being set. (:issue:`2201`) By `Thomas Voigt `_. + - Tests can be run in parallel with pytest-xdist + By `Tony Tung `_. + - Now raises a ValueError when there is a conflict between dimension names and level names of MultiIndex. (:issue:`2299`) -- Follow up the renamings in dask; from dask.ghost to dask.overlap By `Keisuke Fujii `_. +- Follow up the renamings in dask; from dask.ghost to dask.overlap + By `Keisuke Fujii `_. - Now :py:func:`xr.apply_ufunc` raises a ValueError when the size of ``input_core_dims`` is inconsistent with the number of arguments.