From 65e37694250390e1e16f68970a8618460c1b82be Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 11:50:23 -0600 Subject: [PATCH 01/26] Added rename_coords and rename_dims --- xarray/core/dataset.py | 98 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 9 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 3e00640ba60..065972192d1 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2212,12 +2212,12 @@ def interp_like(self, other, method='linear', assume_sorted=False, return ds.interp(numeric_coords, method, assume_sorted, kwargs) # Helper methods for rename() - def _rename_vars(self, name_dict, dims_dict): + def _rename_vars(self, name_dict): variables = OrderedDict() coord_names = set() for k, v in self.variables.items(): name = name_dict.get(k, k) - dims = tuple(dims_dict.get(dim, dim) for dim in v.dims) + dims = tuple(name_dict.get(dim, dim) for dim in v.dims) var = v.copy(deep=False) var.dims = dims if name in variables: @@ -2227,8 +2227,8 @@ def _rename_vars(self, name_dict, dims_dict): coord_names.add(name) return variables, coord_names - def _rename_dims(self, dims_dict): - return {dims_dict.get(k, k): v for k, v in self.dims.items()} + def _rename_dims(self, name_dict): + return {name_dict.get(k, k): v for k, v in self.dims.items()} def _rename_indexes(self, name_dict): if self._indexes is None: @@ -2245,9 +2245,9 @@ def _rename_indexes(self, name_dict): indexes[new_name] = index return indexes - def _rename_all(self, name_dict, dim_dict): - variables, coord_names = self._rename_vars(name_dict, dim_dict) - dims = self._rename_dims(dim_dict) + def _rename_all(self, name_dict): + variables, coord_names = self._rename_vars(name_dict) + dims = self._rename_dims(name_dict) indexes = self._rename_indexes(name_dict) return variables, coord_names, dims, indexes @@ -2274,9 +2274,11 @@ def rename(self, name_dict=None, inplace=None, **names): See Also -------- Dataset.swap_dims + Dataset.rename_coords + Dataset.rename_dims DataArray.rename """ - # TODO: add separate rename_vars and rename_dims methods. + inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): @@ -2285,10 +2287,88 @@ def rename(self, name_dict=None, inplace=None, **names): "variable or dimension in this dataset" % k) variables, coord_names, dims, indexes = self._rename_all( - name_dict=name_dict, dim_dict=name_dict) + name_dict=name_dict) return self._replace(variables, coord_names, dims=dims, indexes=indexes, inplace=inplace) + + def rename_dims(self, name_dict=None, inplace=None, **names): + """Returns a new object with renamed dimensions only. + + Parameters + ---------- + name_dict : dict-like, optional + Dictionary whose keys are current dimension names and + whose values are the desired names. + inplace : bool, optional + If True, rename imensions in-place. Otherwise, + return a new dataset object. + **names, optional + Keyword form of ``name_dict``. + One of name_dict or names must be provided. + + Returns + ------- + renamed : Dataset + Dataset with renamed dimensions. + + See Also + -------- + Dataset.swap_dims + Dataset.rename + Dataset.rename_coords + DataArray.rename + """ + inplace = _check_inplace(inplace) + name_dict = either_dict_or_kwargs(name_dict, names, 'rename') + for k, v in name_dict.items(): + if k not in self.dims: + raise ValueError("cannot rename %r because it is not a " + "dimension in this dataset" % k) + + dims = self._rename_dims(name_dict) + indexes = self._rename_indexes(name_dict) + return self._replace(dims=dims, indexes=indexes, inplace=inplace) + + + def rename_coords(self, name_dict=None, inplace=None, **names): + """Returns a new object with renamed variables and coordinates + + Parameters + ---------- + name_dict : dict-like, optional + Dictionary whose keys are current variable or coordinate names and + whose values are the desired names. + inplace : bool, optional + If True, rename variables and dimensions in-place. Otherwise, + return a new dataset object. + **names, optional + Keyword form of ``name_dict``. + One of name_dict or names must be provided. + + Returns + ------- + renamed : Dataset + Dataset with renamed variables and coordinates + + See Also + -------- + Dataset.swap_dims + Dataset.rename + Dataset.rename_dims + DataArray.rename + """ + + inplace = _check_inplace(inplace) + name_dict = either_dict_or_kwargs(name_dict, names, 'rename') + for k, v in name_dict.items(): + if k not in self: + raise ValueError("cannot rename %r because it is not a " + "variable or coordinate in this dataset" % k) + variables, coord_names = self._rename_vars(name_dict) + return self._replace(variables, coord_names, inplace=inplace) + + def swap_dims(self, dims_dict, inplace=None): """Returns a new object with swapped dimensions. From 4eaeaf52933fe187a40fb8a0dfc3ab67bdc45cff Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 12:02:06 -0600 Subject: [PATCH 02/26] Removed white space from blank lines --- xarray/core/dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 065972192d1..85ab2ba4396 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2278,7 +2278,7 @@ def rename(self, name_dict=None, inplace=None, **names): Dataset.rename_dims DataArray.rename """ - + inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): @@ -2358,7 +2358,7 @@ def rename_coords(self, name_dict=None, inplace=None, **names): Dataset.rename_dims DataArray.rename """ - + inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): From 69c074e38675e86a3b0b006dcb97f7e40735193f Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 12:09:48 -0600 Subject: [PATCH 03/26] Changed rename_coords to rename_vars --- 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 85ab2ba4396..eab5bf0e720 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2331,7 +2331,7 @@ def rename_dims(self, name_dict=None, inplace=None, **names): return self._replace(dims=dims, indexes=indexes, inplace=inplace) - def rename_coords(self, name_dict=None, inplace=None, **names): + def rename_vars(self, name_dict=None, inplace=None, **names): """Returns a new object with renamed variables and coordinates Parameters From 7cd26ceb14817f2d628a23049e0c73f394197576 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 12:10:51 -0600 Subject: [PATCH 04/26] Changed rename_coords to rename_vars in "See Also" or rename fx --- xarray/core/dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index eab5bf0e720..62b0cf3a66f 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2274,7 +2274,7 @@ def rename(self, name_dict=None, inplace=None, **names): See Also -------- Dataset.swap_dims - Dataset.rename_coords + Dataset.rename_vars Dataset.rename_dims DataArray.rename """ @@ -2316,7 +2316,7 @@ def rename_dims(self, name_dict=None, inplace=None, **names): -------- Dataset.swap_dims Dataset.rename - Dataset.rename_coords + Dataset.rename_vars DataArray.rename """ inplace = _check_inplace(inplace) From bc6d90b0cea8dbb73b40b2ad6ef0e7e93a82a2ba Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 13:54:08 -0600 Subject: [PATCH 05/26] Fixed renaming dimension indexing --- xarray/core/dataset.py | 56 +++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 62b0cf3a66f..de005f02969 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2212,6 +2212,29 @@ def interp_like(self, other, method='linear', assume_sorted=False, return ds.interp(numeric_coords, method, assume_sorted, kwargs) # Helper methods for rename() + def _rename_var_dims_only(self, name_dict): + variables = OrderedDict() + coord_names = set() + for k, v in self.variables.items(): + dims = tuple(name_dict.get(dim, dim) for dim in v.dims) + var = v.copy(deep=False) + var.dims = dims + variables[k] = var + return variables + + def _rename_vars_only(self, name_dict): + variables = OrderedDict() + coord_names = set() + for k, v in self.variables.items(): + name = name_dict.get(k, k) + var = v.copy(deep=False) + if name in variables: + raise ValueError('the new name %r conflicts' % (name,)) + variables[name] = var + if k in self._coord_names: + coord_names.add(name) + return variables, coord_names + def _rename_vars(self, name_dict): variables = OrderedDict() coord_names = set() @@ -2251,7 +2274,7 @@ def _rename_all(self, name_dict): indexes = self._rename_indexes(name_dict) return variables, coord_names, dims, indexes - def rename(self, name_dict=None, inplace=None, **names): + def rename(self, name_dict=None, **names): """Returns a new object with renamed variables and dimensions. Parameters @@ -2259,9 +2282,6 @@ def rename(self, name_dict=None, inplace=None, **names): name_dict : dict-like, optional Dictionary whose keys are current variable or dimension names and whose values are the desired names. - inplace : bool, optional - If True, rename variables and dimensions in-place. Otherwise, - return a new dataset object. **names, optional Keyword form of ``name_dict``. One of name_dict or names must be provided. @@ -2278,8 +2298,6 @@ def rename(self, name_dict=None, inplace=None, **names): Dataset.rename_dims DataArray.rename """ - - inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): if k not in self and k not in self.dims: @@ -2289,10 +2307,10 @@ def rename(self, name_dict=None, inplace=None, **names): variables, coord_names, dims, indexes = self._rename_all( name_dict=name_dict) return self._replace(variables, coord_names, dims=dims, - indexes=indexes, inplace=inplace) + indexes=indexes) - def rename_dims(self, name_dict=None, inplace=None, **names): + def rename_dims(self, name_dict=None, **names): """Returns a new object with renamed dimensions only. Parameters @@ -2300,9 +2318,6 @@ def rename_dims(self, name_dict=None, inplace=None, **names): name_dict : dict-like, optional Dictionary whose keys are current dimension names and whose values are the desired names. - inplace : bool, optional - If True, rename imensions in-place. Otherwise, - return a new dataset object. **names, optional Keyword form of ``name_dict``. One of name_dict or names must be provided. @@ -2319,7 +2334,6 @@ def rename_dims(self, name_dict=None, inplace=None, **names): Dataset.rename_vars DataArray.rename """ - inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): if k not in self.dims: @@ -2327,21 +2341,19 @@ def rename_dims(self, name_dict=None, inplace=None, **names): "dimension in this dataset" % k) dims = self._rename_dims(name_dict) + variables = self._rename_var_dims_only(name_dict) indexes = self._rename_indexes(name_dict) - return self._replace(dims=dims, indexes=indexes, inplace=inplace) + return self._replace(variables, dims=dims, indexes=indexes) - def rename_vars(self, name_dict=None, inplace=None, **names): - """Returns a new object with renamed variables and coordinates + def rename_vars(self, name_dict=None, **names): + """Returns a new object with renamed variables including coordinates Parameters ---------- name_dict : dict-like, optional Dictionary whose keys are current variable or coordinate names and whose values are the desired names. - inplace : bool, optional - If True, rename variables and dimensions in-place. Otherwise, - return a new dataset object. **names, optional Keyword form of ``name_dict``. One of name_dict or names must be provided. @@ -2349,7 +2361,7 @@ def rename_vars(self, name_dict=None, inplace=None, **names): Returns ------- renamed : Dataset - Dataset with renamed variables and coordinates + Dataset with renamed variables including coordinates See Also -------- @@ -2358,15 +2370,13 @@ def rename_vars(self, name_dict=None, inplace=None, **names): Dataset.rename_dims DataArray.rename """ - - inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k, v in name_dict.items(): if k not in self: raise ValueError("cannot rename %r because it is not a " "variable or coordinate in this dataset" % k) - variables, coord_names = self._rename_vars(name_dict) - return self._replace(variables, coord_names, inplace=inplace) + variables, coord_names = self._rename_vars_only(name_dict) + return self._replace(variables, coord_names) def swap_dims(self, dims_dict, inplace=None): From babf193b518e6263d641cbf6d40640b690a0ba17 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 14:29:57 -0600 Subject: [PATCH 06/26] Added testing for rename_vars and rename_dims --- xarray/tests/test_dataset.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 1265f6a337a..5e93d0a2a7d 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2111,6 +2111,20 @@ def test_rename_inplace(self): # check virtual variables assert_array_equal(data['t.dayofyear'], [1, 2, 3]) + def test_rename_dims(self): + original = Dataset({'x':('x',[0,1,2]),'y':('x',[10,11,12]),'z':42}) + expected = Dataset({'x':('x_new',[0,1,2]),'y':('x_new',[10,11,12]),'z':42}) + name_dict = {'x':'x_new'} + actual = original.rename_dims(name_dict) + assert_identical(expected, actual) + + def test_rename_vars(self): + original = Dataset({'x':('x',[0,1,2]),'y':('x',[10,11,12]),'z':42}) + expected = Dataset({'x_new':('x',[0,1,2]),'y':('x',[10,11,12]),'z':42}) + name_dict = {'x':'x_new'} + actual = original.rename_vars(name_dict) + assert_identical(expected, actual) + def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) expected = Dataset({'z': 42}, From 588f9c8fdcd6ea047510f4a82835595e255b9eb9 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 16:47:26 -0600 Subject: [PATCH 07/26] Testing and fx for renaming vars and dims --- xarray.code-workspace | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 xarray.code-workspace diff --git a/xarray.code-workspace b/xarray.code-workspace new file mode 100644 index 00000000000..9f833846be7 --- /dev/null +++ b/xarray.code-workspace @@ -0,0 +1,11 @@ +{ + "folders": [ + { + "path": "/Users/jkent/Documents/esmlab-regrid/xarray/xarray" + }, + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file From 750588360576b75c60e89e9e7344557218b043dc Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 25 Jun 2019 17:02:38 -0600 Subject: [PATCH 08/26] Met pep8 standards --- xarray/core/dataset.py | 58 +++++++++++++++++------------------- xarray/tests/test_dataset.py | 20 ++++++++----- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 30e81b5327c..ea4020d219d 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -149,8 +149,8 @@ def merge_indexes( for n in var_names: var = variables[n] - if (current_index_variable is not None and - var.dims != current_index_variable.dims): + if (current_index_variable is not None + and var.dims != current_index_variable.dims): raise ValueError( "dimension mismatch between %r %s and %r %s" % (dim, current_index_variable.dims, n, var.dims)) @@ -209,8 +209,8 @@ def split_indexes( Not public API. Used in Dataset and DataArray reset_index methods. """ - if (isinstance(dims_or_levels, str) - or not isinstance(dims_or_levels, Sequence)): + if (isinstance(dims_or_levels, str) or + not isinstance(dims_or_levels, Sequence)): dims_or_levels = [dims_or_levels] dim_levels \ @@ -287,8 +287,8 @@ def __len__(self) -> int: return len(self._dataset._variables) - len(self._dataset._coord_names) def __contains__(self, key) -> bool: - return (key in self._dataset._variables and - key not in self._dataset._coord_names) + return (key in self._dataset._variables + and key not in self._dataset._coord_names) def __getitem__(self, key) -> 'DataArray': if key not in self._dataset._coord_names: @@ -1188,8 +1188,8 @@ def identical(self, other): Dataset.equals """ try: - return (utils.dict_equiv(self.attrs, other.attrs) and - self._all_compat(other, 'identical')) + return (utils.dict_equiv(self.attrs, other.attrs) + and self._all_compat(other, 'identical')) except (TypeError, AttributeError): return False @@ -2147,8 +2147,8 @@ def _validate_interp_indexer(x, new_x): # In the case of datetimes, the restrictions placed on indexers # used with interp are stronger than those which are placed on # isel, so we need an additional check after _validate_indexers. - if (_contains_datetime_like_objects(x) and - not _contains_datetime_like_objects(new_x)): + if (_contains_datetime_like_objects(x) + and not _contains_datetime_like_objects(new_x)): raise TypeError('When interpolating over a datetime-like ' 'coordinate, the coordinates to ' 'interpolate to must be either datetime ' @@ -2258,7 +2258,6 @@ def interp_like(self, other, method='linear', assume_sorted=False, # Helper methods for rename() def _rename_var_dims_only(self, name_dict): variables = OrderedDict() - coord_names = set() for k, v in self.variables.items(): dims = tuple(name_dict.get(dim, dim) for dim in v.dims) var = v.copy(deep=False) @@ -2353,7 +2352,6 @@ def rename(self, name_dict=None, **names): return self._replace(variables, coord_names, dims=dims, indexes=indexes) - def rename_dims(self, name_dict=None, **names): """Returns a new object with renamed dimensions only. @@ -2385,11 +2383,10 @@ def rename_dims(self, name_dict=None, **names): "dimension in this dataset" % k) dims = self._rename_dims(name_dict) - variables = self._rename_var_dims_only(name_dict) + variables = self._rename_var_dims_only(name_dict) indexes = self._rename_indexes(name_dict) return self._replace(variables, dims=dims, indexes=indexes) - def rename_vars(self, name_dict=None, **names): """Returns a new object with renamed variables including coordinates @@ -2422,7 +2419,6 @@ def rename_vars(self, name_dict=None, **names): variables, coord_names = self._rename_vars_only(name_dict) return self._replace(variables, coord_names) - def swap_dims(self, dims_dict, inplace=None): """Returns a new object with swapped dimensions. @@ -2550,8 +2546,8 @@ def expand_dims(self, dim=None, axis=None, **dim_kwargs): if d in self.dims: raise ValueError( 'Dimension {dim} already exists.'.format(dim=d)) - if (d in self._variables and - not utils.is_scalar(self._variables[d])): + if (d in self._variables + and not utils.is_scalar(self._variables[d])): raise ValueError( '{dim} already exists as coordinate or' ' variable name.'.format(dim=d)) @@ -3342,9 +3338,9 @@ def reduce(self, func, dim=None, keep_attrs=None, keepdims=False, if not reduce_dims: variables[name] = var else: - if (not numeric_only or - np.issubdtype(var.dtype, np.number) or - (var.dtype == np.bool_)): + if (not numeric_only + or np.issubdtype(var.dtype, np.number) + or (var.dtype == np.bool_)): if len(reduce_dims) == 1: # unpack dimensions for the benefit of functions # like np.argmin which can't handle tuple arguments @@ -3877,8 +3873,8 @@ def diff(self, dim, n=1, label='upper'): for name, var in self.variables.items(): if dim in var.dims: if name in self.data_vars: - variables[name] = (var.isel(**kwargs_end) - - var.isel(**kwargs_start)) + variables[name] = (var.isel(**kwargs_end) + - var.isel(**kwargs_start)) else: variables[name] = var.isel(**kwargs_new) else: @@ -4062,8 +4058,8 @@ def sortby(self, variables, ascending=True): for data_array in aligned_other_vars: if data_array.ndim != 1: raise ValueError("Input DataArray is not 1-D.") - if (data_array.dtype == object and - LooseVersion(np.__version__) < LooseVersion('1.11.0')): + if (data_array.dtype == object + and LooseVersion(np.__version__) < LooseVersion('1.11.0')): raise NotImplementedError( 'sortby uses np.lexsort under the hood, which requires ' 'numpy 1.11.0 or later to support object data-type.') @@ -4139,9 +4135,9 @@ def quantile(self, q, dim=None, interpolation='linear', reduce_dims = [d for d in var.dims if d in dims] if reduce_dims or not var.dims: if name not in self.coords: - if (not numeric_only or - np.issubdtype(var.dtype, np.number) or - var.dtype == np.bool_): + if (not numeric_only + or np.issubdtype(var.dtype, np.number) + or var.dtype == np.bool_): if len(reduce_dims) == var.ndim: # prefer to aggregate over axis=None rather than # axis=(0, 1) if they will be equivalent, because @@ -4257,8 +4253,8 @@ def differentiate(self, coord, edge_order=1, datetime_unit=None): variables = OrderedDict() for k, v in self.variables.items(): - if (k in self.data_vars and dim in v.dims and - k not in self.coords): + if (k in self.data_vars and dim in v.dims + and k not in self.coords): if _contains_datetime_like_objects(v): v = v._to_numeric(datetime_unit=datetime_unit) grad = duck_array_ops.gradient( @@ -4434,8 +4430,8 @@ def filter_by_attrs(self, **kwargs): has_value_flag = False for attr_name, pattern in kwargs.items(): attr_value = variable.attrs.get(attr_name) - if ((callable(pattern) and pattern(attr_value)) or - attr_value == pattern): + if ((callable(pattern) and pattern(attr_value)) + or attr_value == pattern): has_value_flag = True else: has_value_flag = False diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 5e93d0a2a7d..bfb1e79096d 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -409,7 +409,7 @@ def test_properties(self): # change them inadvertently: assert isinstance(ds.dims, utils.Frozen) assert isinstance(ds.dims.mapping, utils.SortedKeysDict) - assert type(ds.dims.mapping.mapping) is dict # noqa + assert isinstance(ds.dims.mapping.mapping, dict) # noqa assert list(ds) == list(ds.data_vars) assert list(ds.keys()) == list(ds.data_vars) @@ -2061,7 +2061,7 @@ def test_rename(self): assert_equal(Variable(dims, v.values, v.attrs), renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding - assert type(v) == type(renamed.variables[k]) # noqa: E721 + assert isinstance(v, type(renamed.variables[k])) # noqa: E721 assert 'var1' not in renamed assert 'dim2' not in renamed @@ -2112,16 +2112,20 @@ def test_rename_inplace(self): assert_array_equal(data['t.dayofyear'], [1, 2, 3]) def test_rename_dims(self): - original = Dataset({'x':('x',[0,1,2]),'y':('x',[10,11,12]),'z':42}) - expected = Dataset({'x':('x_new',[0,1,2]),'y':('x_new',[10,11,12]),'z':42}) - name_dict = {'x':'x_new'} + original = Dataset( + {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) + expected = Dataset( + {'x': ('x_new', [0, 1, 2]), 'y': ('x_new', [10, 11, 12]), 'z': 42}) + name_dict = {'x': 'x_new'} actual = original.rename_dims(name_dict) assert_identical(expected, actual) def test_rename_vars(self): - original = Dataset({'x':('x',[0,1,2]),'y':('x',[10,11,12]),'z':42}) - expected = Dataset({'x_new':('x',[0,1,2]),'y':('x',[10,11,12]),'z':42}) - name_dict = {'x':'x_new'} + original = Dataset( + {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) + expected = Dataset( + {'x_new': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) + name_dict = {'x': 'x_new'} actual = original.rename_vars(name_dict) assert_identical(expected, actual) From 9215908655a9b34a43abdec0071b644f821eb695 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 26 Jun 2019 09:15:30 -0600 Subject: [PATCH 09/26] Undid autopep8 for lines w noqa --- xarray/tests/test_dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index bfb1e79096d..926eb7be818 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -409,7 +409,7 @@ def test_properties(self): # change them inadvertently: assert isinstance(ds.dims, utils.Frozen) assert isinstance(ds.dims.mapping, utils.SortedKeysDict) - assert isinstance(ds.dims.mapping.mapping, dict) # noqa + assert type(ds.dims.mapping.mapping) is dict# noqa assert list(ds) == list(ds.data_vars) assert list(ds.keys()) == list(ds.data_vars) @@ -2061,7 +2061,7 @@ def test_rename(self): assert_equal(Variable(dims, v.values, v.attrs), renamed[k].variable.to_base_variable()) assert v.encoding == renamed[k].encoding - assert isinstance(v, type(renamed.variables[k])) # noqa: E721 + assert type(v) == type(renamed.variables[k]) # noqa: E721 assert 'var1' not in renamed assert 'dim2' not in renamed From 8753345f2ddb3096a7e43572e1e83677f98f06d6 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 26 Jun 2019 10:57:48 -0600 Subject: [PATCH 10/26] Update xarray/tests/test_dataset.py Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> --- xarray/tests/test_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 926eb7be818..01fcb4d6217 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -409,7 +409,7 @@ def test_properties(self): # change them inadvertently: assert isinstance(ds.dims, utils.Frozen) assert isinstance(ds.dims.mapping, utils.SortedKeysDict) - assert type(ds.dims.mapping.mapping) is dict# noqa + assert type(ds.dims.mapping.mapping) is dict # noqa assert list(ds) == list(ds.data_vars) assert list(ds.keys()) == list(ds.data_vars) From b79016071b20b26479f3c0e65ccf95d633f0512b Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 26 Jun 2019 11:27:45 -0600 Subject: [PATCH 11/26] Cleaned up helper fxs and added actual_2 test --- xarray/core/dataset.py | 44 ++++++++++++++++++------------------ xarray/tests/test_dataset.py | 4 ++++ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ea4020d219d..d5ca9ad8191 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2256,41 +2256,41 @@ def interp_like(self, other, method='linear', assume_sorted=False, return ds.interp(numeric_coords, method, assume_sorted, kwargs) # Helper methods for rename() + def _rename_var_dims_helper(name_dict, v): + dims = tuple(name_dict.get(dim, dim) for dim in v.dims) + var = v.copy(deep=False) + var.dims = dims + return var + def _rename_var_dims_only(self, name_dict): variables = OrderedDict() for k, v in self.variables.items(): - dims = tuple(name_dict.get(dim, dim) for dim in v.dims) - var = v.copy(deep=False) - var.dims = dims - variables[k] = var + variables[k] = _rename_var_dims_helper(name_dict, v) return variables + def _rename_vars_helper(name_dict, var): + name = name_dict.get(k, k) + if name in variables: + raise ValueError('the new name %r conflicts' % (name,)) + variables[name] = var + if k in self._coord_names: + coord_names.add(name) + return variables, coord_names + def _rename_vars_only(self, name_dict): variables = OrderedDict() coord_names = set() for k, v in self.variables.items(): - name = name_dict.get(k, k) var = v.copy(deep=False) - if name in variables: - raise ValueError('the new name %r conflicts' % (name,)) - variables[name] = var - if k in self._coord_names: - coord_names.add(name) + variables, coord_names = _rename_vars_helper(name_dict, var) return variables, coord_names def _rename_vars(self, name_dict): variables = OrderedDict() coord_names = set() for k, v in self.variables.items(): - name = name_dict.get(k, k) - dims = tuple(name_dict.get(dim, dim) for dim in v.dims) - var = v.copy(deep=False) - var.dims = dims - if name in variables: - raise ValueError('the new name %r conflicts' % (name,)) - variables[name] = var - if k in self._coord_names: - coord_names.add(name) + var = _rename_var_dims_helper(name_dict, v) + variables, coord_names = _rename_vars_helper(name_dict, var) return variables, coord_names def _rename_dims(self, name_dict): @@ -2342,7 +2342,7 @@ def rename(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for k, v in name_dict.items(): + for in name_dict.items(): if k not in self and k not in self.dims: raise ValueError("cannot rename %r because it is not a " "variable or dimension in this dataset" % k) @@ -2377,7 +2377,7 @@ def rename_dims(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for k, v in name_dict.items(): + for k in name_dict.items(): if k not in self.dims: raise ValueError("cannot rename %r because it is not a " "dimension in this dataset" % k) @@ -2412,7 +2412,7 @@ def rename_vars(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for k, v in name_dict.items(): + for k in name_dict.items(): if k not in self: raise ValueError("cannot rename %r because it is not a " "variable or coordinate in this dataset" % k) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 926eb7be818..952d039f06f 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2119,6 +2119,8 @@ def test_rename_dims(self): name_dict = {'x': 'x_new'} actual = original.rename_dims(name_dict) assert_identical(expected, actual) + actual_2 = original.rename_dims(**name_dict) + assert_identical(expected, actual_2) def test_rename_vars(self): original = Dataset( @@ -2128,6 +2130,8 @@ def test_rename_vars(self): name_dict = {'x': 'x_new'} actual = original.rename_vars(name_dict) assert_identical(expected, actual) + actual_2 = original.rename_vars(**name_dict) + assert_identical(expected, actual_2) def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) From ae0deb1a7495d16996c33421891510c4266640b7 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 26 Jun 2019 11:43:43 -0600 Subject: [PATCH 12/26] Update xarray/core/dataset.py Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> --- 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 d5ca9ad8191..2339e4260f4 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2342,7 +2342,7 @@ def rename(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for in name_dict.items(): + for x in name_dict.keys(): if k not in self and k not in self.dims: raise ValueError("cannot rename %r because it is not a " "variable or dimension in this dataset" % k) From cf28ddea5485e7ae715162d6e69fc7db852e5014 Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 26 Jun 2019 11:45:53 -0600 Subject: [PATCH 13/26] Update xarray/core/dataset.py Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> --- 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 2339e4260f4..04a5ae17fca 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2412,7 +2412,7 @@ def rename_vars(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for k in name_dict.items(): + for k in name_dict.keys(): if k not in self: raise ValueError("cannot rename %r because it is not a " "variable or coordinate in this dataset" % k) From a73390df548877c9da98c415ee7e4c6f8522dba5 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 26 Jun 2019 11:54:11 -0600 Subject: [PATCH 14/26] deleted misc file --- xarray.code-workspace | 11 ----------- xarray/core/dataset.py | 4 ++-- 2 files changed, 2 insertions(+), 13 deletions(-) delete mode 100644 xarray.code-workspace diff --git a/xarray.code-workspace b/xarray.code-workspace deleted file mode 100644 index 9f833846be7..00000000000 --- a/xarray.code-workspace +++ /dev/null @@ -1,11 +0,0 @@ -{ - "folders": [ - { - "path": "/Users/jkent/Documents/esmlab-regrid/xarray/xarray" - }, - { - "path": "." - } - ], - "settings": {} -} \ No newline at end of file diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index d5ca9ad8191..e3dd11283c2 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2342,7 +2342,7 @@ def rename(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for in name_dict.items(): + for k in name_dict.items(): if k not in self and k not in self.dims: raise ValueError("cannot rename %r because it is not a " "variable or dimension in this dataset" % k) @@ -2387,7 +2387,7 @@ def rename_dims(self, name_dict=None, **names): indexes = self._rename_indexes(name_dict) return self._replace(variables, dims=dims, indexes=indexes) - def rename_vars(self, name_dict=None, **names): + def rename_var(self, name_dict=None, **names): """Returns a new object with renamed variables including coordinates Parameters From a0d4ca1bff67a83d9d15a086726274d45d071f4b Mon Sep 17 00:00:00 2001 From: Julia Kent <46687291+jukent@users.noreply.github.com> Date: Wed, 26 Jun 2019 11:59:17 -0600 Subject: [PATCH 15/26] Update xarray/core/dataset.py Co-Authored-By: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> --- 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 04a5ae17fca..829f91f64f5 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2342,7 +2342,7 @@ def rename(self, name_dict=None, **names): DataArray.rename """ name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for x in name_dict.keys(): + for k in name_dict.keys(): if k not in self and k not in self.dims: raise ValueError("cannot rename %r because it is not a " "variable or dimension in this dataset" % k) From bdfd181f6c5301c6a82e3541df44a3d9632de50c Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 26 Jun 2019 13:08:50 -0600 Subject: [PATCH 16/26] _rename_var_dims_helper undefined test --- xarray/core/dataset.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 6f040e5975b..ea836ad39bf 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2256,19 +2256,13 @@ def interp_like(self, other, method='linear', assume_sorted=False, return ds.interp(numeric_coords, method, assume_sorted, kwargs) # Helper methods for rename() - def _rename_var_dims_helper(name_dict, v): + def _rename_var_dims_helper(self, name_dict, v): dims = tuple(name_dict.get(dim, dim) for dim in v.dims) var = v.copy(deep=False) var.dims = dims return var - def _rename_var_dims_only(self, name_dict): - variables = OrderedDict() - for k, v in self.variables.items(): - variables[k] = _rename_var_dims_helper(name_dict, v) - return variables - - def _rename_vars_helper(name_dict, var): + def _rename_vars_helper(self, name_dict, var): name = name_dict.get(k, k) if name in variables: raise ValueError('the new name %r conflicts' % (name,)) @@ -2277,6 +2271,12 @@ def _rename_vars_helper(name_dict, var): coord_names.add(name) return variables, coord_names + def _rename_var_dims_only(self, name_dict): + variables = OrderedDict() + for k, v in self.variables.items(): + variables[k] = _rename_var_dims_helper(name_dict, v) + return variables + def _rename_vars_only(self, name_dict): variables = OrderedDict() coord_names = set() From a77820cd4d2e16dc68f6ca5c8ea0a2cfb7f9a3de Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Wed, 26 Jun 2019 16:37:47 -0600 Subject: [PATCH 17/26] Use separate rename_dims and rename_vars dictionaries --- xarray/core/dataset.py | 81 +++++++++++++----------------------- xarray/tests/test_dataset.py | 10 ++--- 2 files changed, 35 insertions(+), 56 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index ea836ad39bf..1e99de879a4 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2256,41 +2256,18 @@ def interp_like(self, other, method='linear', assume_sorted=False, return ds.interp(numeric_coords, method, assume_sorted, kwargs) # Helper methods for rename() - def _rename_var_dims_helper(self, name_dict, v): - dims = tuple(name_dict.get(dim, dim) for dim in v.dims) - var = v.copy(deep=False) - var.dims = dims - return var - - def _rename_vars_helper(self, name_dict, var): - name = name_dict.get(k, k) - if name in variables: - raise ValueError('the new name %r conflicts' % (name,)) - variables[name] = var - if k in self._coord_names: - coord_names.add(name) - return variables, coord_names - - def _rename_var_dims_only(self, name_dict): - variables = OrderedDict() - for k, v in self.variables.items(): - variables[k] = _rename_var_dims_helper(name_dict, v) - return variables - - def _rename_vars_only(self, name_dict): + def _rename_vars(self, name_dict, dims_dict): variables = OrderedDict() coord_names = set() for k, v in self.variables.items(): var = v.copy(deep=False) - variables, coord_names = _rename_vars_helper(name_dict, var) - return variables, coord_names - - def _rename_vars(self, name_dict): - variables = OrderedDict() - coord_names = set() - for k, v in self.variables.items(): - var = _rename_var_dims_helper(name_dict, v) - variables, coord_names = _rename_vars_helper(name_dict, var) + var.dims = tuple(dims_dict.get(dim, dim) for dim in v.dims) + name = name_dict.get(k, k) + if name in variables: + raise ValueError('the new name %r conflicts' % (name,)) + variables[name] = var + if k in self._coord_names: + coord_names.add(name) return variables, coord_names def _rename_dims(self, name_dict): @@ -2311,9 +2288,9 @@ def _rename_indexes(self, name_dict): indexes[new_name] = index return indexes - def _rename_all(self, name_dict): - variables, coord_names = self._rename_vars(name_dict) - dims = self._rename_dims(name_dict) + def _rename_all(self, name_dict, dims_dict): + variables, coord_names = self._rename_vars(name_dict, dims_dict) + dims = self._rename_dims(dims_dict) indexes = self._rename_indexes(name_dict) return variables, coord_names, dims, indexes @@ -2348,21 +2325,21 @@ def rename(self, name_dict=None, **names): "variable or dimension in this dataset" % k) variables, coord_names, dims, indexes = self._rename_all( - name_dict=name_dict) + name_dict=name_dict, dims_dict=name_dict) return self._replace(variables, coord_names, dims=dims, indexes=indexes) - def rename_dims(self, name_dict=None, **names): + def rename_dims(self, dims_dict=None, **dims): """Returns a new object with renamed dimensions only. Parameters ---------- - name_dict : dict-like, optional + dims_dict : dict-like, optional Dictionary whose keys are current dimension names and whose values are the desired names. - **names, optional - Keyword form of ``name_dict``. - One of name_dict or names must be provided. + **dims, optional + Keyword form of ``dims_dict``. + One of dims_dict or dims must be provided. Returns ------- @@ -2376,18 +2353,18 @@ def rename_dims(self, name_dict=None, **names): Dataset.rename_vars DataArray.rename """ - name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for k in name_dict.items(): + dims_dict = either_dict_or_kwargs(dims_dict, dims, 'rename_dims') + for k in dims_dict: if k not in self.dims: raise ValueError("cannot rename %r because it is not a " "dimension in this dataset" % k) - dims = self._rename_dims(name_dict) - variables = self._rename_var_dims_only(name_dict) - indexes = self._rename_indexes(name_dict) - return self._replace(variables, dims=dims, indexes=indexes) + variables, coord_names, dims, indexes = self._rename_all( + name_dict={}, dims_dict=dims_dict) + return self._replace(variables, coord_names, dims=dims, + indexes=indexes) - def rename_var(self, name_dict=None, **names): + def rename_vars(self, name_dict=None, **names): """Returns a new object with renamed variables including coordinates Parameters @@ -2411,13 +2388,15 @@ def rename_var(self, name_dict=None, **names): Dataset.rename_dims DataArray.rename """ - name_dict = either_dict_or_kwargs(name_dict, names, 'rename') - for k in name_dict.keys(): + name_dict = either_dict_or_kwargs(name_dict, names, 'rename_vars') + for k in name_dict: if k not in self: raise ValueError("cannot rename %r because it is not a " "variable or coordinate in this dataset" % k) - variables, coord_names = self._rename_vars_only(name_dict) - return self._replace(variables, coord_names) + variables, coord_names, dims, indexes = self._rename_all( + name_dict=name_dict, dims_dict={}) + return self._replace(variables, coord_names, dims=dims, + indexes=indexes) def swap_dims(self, dims_dict, inplace=None): """Returns a new object with swapped dimensions. diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index b1798252cee..f7c452cd5c7 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2116,18 +2116,18 @@ def test_rename_dims(self): {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) expected = Dataset( {'x': ('x_new', [0, 1, 2]), 'y': ('x_new', [10, 11, 12]), 'z': 42}) - name_dict = {'x': 'x_new'} - actual = original.rename_dims(name_dict) + dims_dict = {'x': 'x_new','y':'y_new'} + actual = original.rename_dims(dims_dict) assert_identical(expected, actual) - actual_2 = original.rename_dims(**name_dict) + actual_2 = original.rename_dims(**dims_dict) assert_identical(expected, actual_2) def test_rename_vars(self): original = Dataset( {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) expected = Dataset( - {'x_new': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) - name_dict = {'x': 'x_new'} + {'x_new': ('x', [0, 1, 2]), 'y_new': ('x', [10, 11, 12]), 'z': 42}) + name_dict = {'x': 'x_new','y':'y_new'} actual = original.rename_vars(name_dict) assert_identical(expected, actual) actual_2 = original.rename_vars(**name_dict) From c19ddb395bea8e72bbf37f85917559a22998c3bb Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 27 Jun 2019 12:32:51 -0600 Subject: [PATCH 18/26] Fixed documentation and added inplace back --- doc/api.rst | 2 ++ doc/whats-new.rst | 6 ++++++ xarray/core/dataset.py | 3 ++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/api.rst b/doc/api.rst index e1f9238c815..88907e57ced 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -87,6 +87,8 @@ Dataset contents Dataset.pipe Dataset.merge Dataset.rename + Dataset.rename_vars + Dataset.rename_dims Dataset.swap_dims Dataset.expand_dims Dataset.drop diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 39521fe4915..f69c0e3028e 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -59,6 +59,12 @@ Enhancements - Combining datasets along N dimensions: Datasets can now be combined along any number of dimensions, instead of just a one-dimensional list of datasets. +- Renaming variables and dimensions independently: + Datasets with coordinate dimensions can now have only their dimension + (using rename_dim) or only their coordinate (using rename_vars) renamed + instead of the rename function applyingto both. (:issue:`3026`) + By `Julia Kent `_. + The new ``combine_manual`` will accept the datasets as a a nested list-of-lists, and combine by applying a series of concat and merge diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 1e99de879a4..57877e3e124 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2294,7 +2294,7 @@ def _rename_all(self, name_dict, dims_dict): indexes = self._rename_indexes(name_dict) return variables, coord_names, dims, indexes - def rename(self, name_dict=None, **names): + def rename(self, name_dict=None, inplace=None, **names): """Returns a new object with renamed variables and dimensions. Parameters @@ -2318,6 +2318,7 @@ def rename(self, name_dict=None, **names): Dataset.rename_dims DataArray.rename """ + inplace = _check_inplace(inplace) name_dict = either_dict_or_kwargs(name_dict, names, 'rename') for k in name_dict.keys(): if k not in self and k not in self.dims: From ef5cc66af7327e4e751e8a9a1203bc86aeff8d94 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 27 Jun 2019 12:43:20 -0600 Subject: [PATCH 19/26] removing changes from rename --- xarray/core/dataset.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 57877e3e124..93cdcabd763 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -2302,6 +2302,9 @@ def rename(self, name_dict=None, inplace=None, **names): name_dict : dict-like, optional Dictionary whose keys are current variable or dimension names and whose values are the desired names. + inplace : bool, optional + If True, rename variables and dimensions in-place. Otherwise, + return a new dataset object. **names, optional Keyword form of ``name_dict``. One of name_dict or names must be provided. @@ -2328,7 +2331,7 @@ def rename(self, name_dict=None, inplace=None, **names): variables, coord_names, dims, indexes = self._rename_all( name_dict=name_dict, dims_dict=name_dict) return self._replace(variables, coord_names, dims=dims, - indexes=indexes) + indexes=indexes, inplace=inplace) def rename_dims(self, dims_dict=None, **dims): """Returns a new object with renamed dimensions only. From 1a08c51d46bb0e83dfc70f9f9d6abfd0d3073755 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 27 Jun 2019 14:12:08 -0600 Subject: [PATCH 20/26] removed test set up to fail (will add back) --- xarray/tests/test_dataset.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index f7c452cd5c7..fd87b31f825 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2116,7 +2116,7 @@ def test_rename_dims(self): {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) expected = Dataset( {'x': ('x_new', [0, 1, 2]), 'y': ('x_new', [10, 11, 12]), 'z': 42}) - dims_dict = {'x': 'x_new','y':'y_new'} + dims_dict = {'x': 'x_new'} actual = original.rename_dims(dims_dict) assert_identical(expected, actual) actual_2 = original.rename_dims(**dims_dict) @@ -2126,8 +2126,8 @@ def test_rename_vars(self): original = Dataset( {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) expected = Dataset( - {'x_new': ('x', [0, 1, 2]), 'y_new': ('x', [10, 11, 12]), 'z': 42}) - name_dict = {'x': 'x_new','y':'y_new'} + {'x_new': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) + name_dict = {'x': 'x_new'} actual = original.rename_vars(name_dict) assert_identical(expected, actual) actual_2 = original.rename_vars(**name_dict) From 97c4e85005bf9d679d783749dd7418b51604c90d Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Thu, 27 Jun 2019 16:29:06 -0600 Subject: [PATCH 21/26] fixed coord vs variable in test rename_ --- xarray/tests/test_dataset.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index fd87b31f825..292769dee35 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2116,6 +2116,7 @@ def test_rename_dims(self): {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) expected = Dataset( {'x': ('x_new', [0, 1, 2]), 'y': ('x_new', [10, 11, 12]), 'z': 42}) + expected = expected.set_coords('x') dims_dict = {'x': 'x_new'} actual = original.rename_dims(dims_dict) assert_identical(expected, actual) @@ -2127,6 +2128,7 @@ def test_rename_vars(self): {'x': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) expected = Dataset( {'x_new': ('x', [0, 1, 2]), 'y': ('x', [10, 11, 12]), 'z': 42}) + expected = expected.set_coords('x_new') name_dict = {'x': 'x_new'} actual = original.rename_vars(name_dict) assert_identical(expected, actual) From caad102c57b4dad279ab46d7a83e8b74fe441c64 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Mon, 1 Jul 2019 15:24:43 -0600 Subject: [PATCH 22/26] Moved rename_var to under new fx/methods --- doc/whats-new.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1b016b973b7..e85829aae29 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -85,6 +85,12 @@ New functions/methods By `Guido Imperiale `_ and `Maximilian Roos `_. +- Renaming variables and dimensions independently: + Datasets with coordinate dimensions can now have only their dimension + (using rename_dim) or only their coordinate (using rename_vars) renamed + instead of the rename function applyingto both. (:issue:`3026`) + By `Julia Kent `_. + Enhancements to existing functionality ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -102,12 +108,7 @@ Enhancements to existing functionality accept a keyword argument ``restore_coord_dims`` which keeps the order of the dimensions of multi-dimensional coordinates intact (:issue:`1856`). By `Peter Hausamann `_. - - Renaming variables and dimensions independently: - Datasets with coordinate dimensions can now have only their dimension - (using rename_dim) or only their coordinate (using rename_vars) renamed - instead of the rename function applyingto both. (:issue:`3026`) - By `Julia Kent `_. - - Clean up Python 2 compatibility in code (:issue:`2950`) +- Clean up Python 2 compatibility in code (:issue:`2950`) By `Guido Imperiale `_. - Implement ``load_dataset()`` and ``load_dataarray()`` as alternatives to ``open_dataset()`` and ``open_dataarray()`` to open, load into memory, From 09c801a1ea472cc0497daf878bc10c84f9d12e64 Mon Sep 17 00:00:00 2001 From: Deepak Cherian Date: Mon, 1 Jul 2019 15:59:41 -0600 Subject: [PATCH 23/26] Update whats-new.rst --- doc/whats-new.rst | 36 ++++++------------------------------ 1 file changed, 6 insertions(+), 30 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e85829aae29..034461e0ded 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -21,6 +21,12 @@ v0.12.3 (unreleased) Enhancements ~~~~~~~~~~~~ +- Renaming variables and dimensions independently: + Datasets with coordinate dimensions can now have only their dimension + (using rename_dim) or only their coordinate (using rename_vars) renamed + instead of the rename function applyingto both. (:issue:`3026`) + By `Julia Kent `_. + Bug fixes ~~~~~~~~~ @@ -85,12 +91,6 @@ New functions/methods By `Guido Imperiale `_ and `Maximilian Roos `_. -- Renaming variables and dimensions independently: - Datasets with coordinate dimensions can now have only their dimension - (using rename_dim) or only their coordinate (using rename_vars) renamed - instead of the rename function applyingto both. (:issue:`3026`) - By `Julia Kent `_. - Enhancements to existing functionality ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -110,30 +110,6 @@ Enhancements to existing functionality By `Peter Hausamann `_. - Clean up Python 2 compatibility in code (:issue:`2950`) By `Guido Imperiale `_. -- Implement ``load_dataset()`` and ``load_dataarray()`` as alternatives to - ``open_dataset()`` and ``open_dataarray()`` to open, load into memory, - and close files, returning the Dataset or DataArray. These functions are - helpful for avoiding file-lock errors when trying to write to files opened - using ``open_dataset()`` or ``open_dataarray()``. (:issue:`2887`) - By `Dan Nowacki `_. -- Combining datasets along N dimensions: - Datasets can now be combined along any number of dimensions, - instead of just a one-dimensional list of datasets. - - The new ``combine_manual`` will accept the datasets as a a nested - list-of-lists, and combine by applying a series of concat and merge - operations. The new ``combine_auto`` will instead use the dimension - coordinates of the datasets to order them. - - ``open_mfdataset`` can use either ``combine_manual`` or ``combine_auto`` to - combine datasets along multiple dimensions, by specifying the argument - `combine='manual'` or `combine='auto'`. - - This means that the original function ``auto_combine`` is being deprecated. - To avoid FutureWarnings switch to using `combine_manual` or `combine_auto`, - (or set the `combine` argument in `open_mfdataset`). (:issue:`2159`) - By `Tom Nicholas `_. - - Better warning message when supplying invalid objects to ``xr.merge`` (:issue:`2948`). By `Mathias Hauser `_. - Add ``errors`` keyword argument to :py:meth:`Dataset.drop` and :py:meth:`Dataset.drop_dims` From f4e147987f147db791a4793744b7c9355f662fa0 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 2 Jul 2019 11:29:13 -0600 Subject: [PATCH 24/26] use pytest.raises to test for ValueError --- xarray/tests/test_dataset.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 0603aa03007..f87b70fa2bb 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2174,6 +2174,11 @@ def test_rename_dims(self): assert_identical(expected, actual) actual_2 = original.rename_dims(**dims_dict) assert_identical(expected, actual_2) + + #Test to raise ValueError + dims_dict_bad = {'x_bad':'x_new'} + with pytest.raises(ValueError): + failed = original.rename_dims(dims_dict_bad) def test_rename_vars(self): original = Dataset( @@ -2187,6 +2192,11 @@ def test_rename_vars(self): actual_2 = original.rename_vars(**name_dict) assert_identical(expected, actual_2) + #Test to raise ValueError + names_dict_bad = {'x_bad': 'x_new'} + with pytest.raises(ValueError): + failed = original.rename_vars(names_dict_bad) + def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) expected = Dataset({'z': 42}, From c97ff0d501165d7cb350f2da32e4166cb12cbfe5 Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 2 Jul 2019 12:20:33 -0600 Subject: [PATCH 25/26] did not assign failed --- xarray/tests/test_dataset.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index f87b70fa2bb..ba099aedb1c 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2178,7 +2178,7 @@ def test_rename_dims(self): #Test to raise ValueError dims_dict_bad = {'x_bad':'x_new'} with pytest.raises(ValueError): - failed = original.rename_dims(dims_dict_bad) + original.rename_dims(dims_dict_bad) def test_rename_vars(self): original = Dataset( @@ -2195,7 +2195,7 @@ def test_rename_vars(self): #Test to raise ValueError names_dict_bad = {'x_bad': 'x_new'} with pytest.raises(ValueError): - failed = original.rename_vars(names_dict_bad) + original.rename_vars(names_dict_bad) def test_swap_dims(self): original = Dataset({'x': [1, 2, 3], 'y': ('x', list('abc')), 'z': 42}) From b3232ad9f140bc622b5cda8868331bc3a4c9b8fc Mon Sep 17 00:00:00 2001 From: Julia Kent Date: Tue, 2 Jul 2019 12:28:40 -0600 Subject: [PATCH 26/26] pep8 compliance --- xarray/tests/test_dataset.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index ba099aedb1c..1815ecf3ea2 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2174,9 +2174,9 @@ def test_rename_dims(self): assert_identical(expected, actual) actual_2 = original.rename_dims(**dims_dict) assert_identical(expected, actual_2) - - #Test to raise ValueError - dims_dict_bad = {'x_bad':'x_new'} + + # Test to raise ValueError + dims_dict_bad = {'x_bad': 'x_new'} with pytest.raises(ValueError): original.rename_dims(dims_dict_bad) @@ -2192,7 +2192,7 @@ def test_rename_vars(self): actual_2 = original.rename_vars(**name_dict) assert_identical(expected, actual_2) - #Test to raise ValueError + # Test to raise ValueError names_dict_bad = {'x_bad': 'x_new'} with pytest.raises(ValueError): original.rename_vars(names_dict_bad)