Skip to content

Behavior of ds.rename when old and new name are the same #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spencerahill opened this issue Jan 26, 2016 · 2 comments
Closed

Behavior of ds.rename when old and new name are the same #724

spencerahill opened this issue Jan 26, 2016 · 2 comments

Comments

@spencerahill
Copy link
Contributor

Before #648, passing Dataset.rename a name_dict whose keys and values were identical did not raise an exception. Now, however, it raises a ValueError:

arr = xr.DataArray(range(2), name='arrname')
ds = arr.to_dataset()
ds.rename({'dim_0':'dim_0'})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-a5b851f9fb10> in <module>()
----> 1 ds.rename({'dim_0':'dim_0'})

/Users/spencerahill/anaconda/lib/python2.7/site-packages/xarray/core/dataset.pyc in rename(self, name_dict, inplace)
   1245                                  "variable in this dataset" % k)
   1246             if v in self:
-> 1247                 raise ValueError('the new name %r already exists' % v)
   1248
   1249         variables = OrderedDict()

ValueError: the new name 'dim_0' already exists

This is easy enough to handle with a try/except clause in my own code, but it would be nice (for me at least) for rename to not raise anything for these cases. Since the result is simply the original Dataset unchanged, this could be implemented by replacing line 1246 with if v in self and k != v:

The use case is that we have data coming from multiple sources, often with differing internal names for coordinates and variables, and we're programmatically forcing them to have consistent names via rename. Sometimes, a piece of data already has the name that we are after, hence the non-rename rename. Thanks!

@shoyer
Copy link
Member

shoyer commented Jan 26, 2016

I agree, this should be OK.

A pull request with this fix would be greatly appreciated! Be sure to add an appropriate test in xarray/test/test_dataset.py.

@spencerahill
Copy link
Contributor Author

Sure, will do in the coming days or over the weekend. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants