From 31ae8281cbe3252a194a75273a1aee22a4cd64a6 Mon Sep 17 00:00:00 2001 From: Keewis Date: Thu, 19 Dec 2019 16:58:26 +0100 Subject: [PATCH 1/5] check we raise an error if the name already exists --- xarray/tests/test_dataset.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 7db1911621b..05b5d7e2a68 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2444,6 +2444,9 @@ def test_rename_dims(self): with pytest.raises(ValueError): original.rename_dims(dims_dict_bad) + with pytest.raises(ValueError): + original.rename_dims({"x": "z"}) + def test_rename_vars(self): original = Dataset({"x": ("x", [0, 1, 2]), "y": ("x", [10, 11, 12]), "z": 42}) expected = Dataset( From 90b570895b86dc8328230c180066ce2c51c77d19 Mon Sep 17 00:00:00 2001 From: Keewis Date: Thu, 19 Dec 2019 17:07:35 +0100 Subject: [PATCH 2/5] raise if the new name already exists and point to swap_dims --- xarray/core/dataset.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 6be06fed117..2a08b410eef 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2809,12 +2809,17 @@ def rename_dims( DataArray.rename """ dims_dict = either_dict_or_kwargs(dims_dict, dims, "rename_dims") - for k in dims_dict: + for k, v in dims_dict.items(): if k not in self.dims: raise ValueError( "cannot rename %r because it is not a " "dimension in this dataset" % k ) + if v in self.dims or v in self: + raise ValueError( + f"cannot rename {k} to {v} because {v} already exists. " + "Try using swap_dims instead." + ) variables, coord_names, sizes, indexes = self._rename_all( name_dict={}, dims_dict=dims_dict From 500f3e8aaf6cf1839887ee81f8be80883add9509 Mon Sep 17 00:00:00 2001 From: Keewis Date: Thu, 19 Dec 2019 17:30:39 +0100 Subject: [PATCH 3/5] update the documentation --- xarray/core/dataset.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 2a08b410eef..0fb351aa544 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2791,7 +2791,8 @@ def rename_dims( ---------- dims_dict : dict-like, optional Dictionary whose keys are current dimension names and - whose values are the desired names. + whose values are the desired names. The desired names must + not exist already. **dims, optional Keyword form of ``dims_dict``. One of dims_dict or dims must be provided. @@ -2817,7 +2818,7 @@ def rename_dims( ) if v in self.dims or v in self: raise ValueError( - f"cannot rename {k} to {v} because {v} already exists. " + f"Cannot rename {k} to {v} because {v} already exists. " "Try using swap_dims instead." ) From c1d0649ccd23efc811734eb4395063ee363634e7 Mon Sep 17 00:00:00 2001 From: Keewis Date: Thu, 19 Dec 2019 17:35:20 +0100 Subject: [PATCH 4/5] whats-new.rst --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index fe05a4d2c21..ccadb21fe80 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -53,6 +53,9 @@ Bug fixes By `Tom Augspurger `_. - Ensure :py:meth:`Dataset.quantile`, :py:meth:`DataArray.quantile` issue the correct error when ``q`` is out of bounds (:issue:`3634`) by `Mathias Hauser `_. +- Raise an error when trying to use :py:meth:`Dataset.rename_dims` to + rename to an existing name (:issue:`3438`, :pull:`3645`) + By `Justus Magin `_. Documentation ~~~~~~~~~~~~~ From 5cd2aeaf37eecabdade443f22453c751a7d89782 Mon Sep 17 00:00:00 2001 From: keewis Date: Wed, 8 Jan 2020 19:23:06 +0100 Subject: [PATCH 5/5] fix the docstring of rename_dims Co-Authored-By: Deepak Cherian --- xarray/core/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 0a455f85dac..63e649dc6bb 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2799,7 +2799,7 @@ def rename_dims( dims_dict : dict-like, optional Dictionary whose keys are current dimension names and whose values are the desired names. The desired names must - not exist already. + not be the name of an existing dimension or Variable in the Dataset. **dims, optional Keyword form of ``dims_dict``. One of dims_dict or dims must be provided.