Skip to content

Commit

Permalink
Fix inconsistent behaviour for xarray.DataArray.rename (#1178)
Browse files Browse the repository at this point in the history
* Changes `DataArray.rename` to a more explicit behavior.

* Changes to only one argument for `DataArray.rename`.

* Rollback of argument name of `DataArray.rename`.

* Removes copy operation on dict, because it is no longer modified.

* Adds attribution.
  • Loading branch information
magonser authored and shoyer committed Dec 23, 2016
1 parent 0fd2e5b commit bb12c69
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
5 changes: 5 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ v0.9.0 (unreleased)
Breaking changes
~~~~~~~~~~~~~~~~

- ``DataArray.rename()`` behavior changed to strictly change the ``DataArray.name``
if called with string argument, or strictly change coordinate names if called with
dict-like argument.
By `Markus Gonser <https://github.com/magonser>`_.

- By default ``to_netcdf()`` add a ``_FillValue = NaN`` attributes to float types.
By `Frederic Laliberte <https://github.com/laliberte>`_.

Expand Down
16 changes: 6 additions & 10 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,15 +789,16 @@ def reindex(self, method=None, tolerance=None, copy=True, **indexers):
return self._from_temp_dataset(ds)

def rename(self, new_name_or_name_dict):
"""Returns a new DataArray with renamed coordinates and/or a new name.
"""Returns a new DataArray with renamed coordinates or a new name.
Parameters
----------
new_name_or_name_dict : str or dict-like
If the argument is dict-like, it it used as a mapping from old
names to new names for coordinates (and/or this array itself).
Otherwise, use the argument as the new name for this array.
names to new names for coordinates. Otherwise, use the argument
as the new name for this array.
Returns
-------
Expand All @@ -810,13 +811,8 @@ def rename(self, new_name_or_name_dict):
DataArray.swap_dims
"""
if utils.is_dict_like(new_name_or_name_dict):
name_dict = new_name_or_name_dict.copy()
if self.name in self.dims:
name = name_dict.get(self.name, self.name)
else:
name = name_dict.pop(self.name, self.name)
dataset = self._to_temp_dataset().rename(name_dict)
return self._from_temp_dataset(dataset, name)
dataset = self._to_temp_dataset().rename(new_name_or_name_dict)
return self._from_temp_dataset(dataset)
else:
return self._replace(name=new_name_or_name_dict)

Expand Down
8 changes: 1 addition & 7 deletions xarray/test/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,13 +893,7 @@ def test_rename(self):
renamed.to_dataset(), self.ds.rename({'foo': 'bar'}))
self.assertEqual(renamed.name, 'bar')

renamed = self.dv.rename({'foo': 'bar'})
self.assertDatasetIdentical(
renamed.to_dataset(), self.ds.rename({'foo': 'bar'}))
self.assertEqual(renamed.name, 'bar')

# regression to #1116
renamed = self.dv.x.rename({'x': 'z'})
renamed = self.dv.x.rename({'x': 'z'}).rename('z')
self.assertDatasetIdentical(
renamed, self.ds.rename({'x': 'z'}).z)
self.assertEqual(renamed.name, 'z')
Expand Down

0 comments on commit bb12c69

Please sign in to comment.