Skip to content

Index.copy() ignores name/names arguments #14302

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
chazmo03 opened this issue Sep 26, 2016 · 1 comment
Closed

Index.copy() ignores name/names arguments #14302

chazmo03 opened this issue Sep 26, 2016 · 1 comment
Labels
Milestone

Comments

@chazmo03
Copy link

Passing in a name or names arguments to Index.copy doesn't set the name on the resulting index. This fails to work as the docs indicate on both Index and MultiIndex.

import pandas as pd
index = pd.Index([1,2], name='MyName')
print index.name

cloned_index_1 = index.copy(name='NewName')
print cloned_index_1.name

cloned_index_2 = index.copy(names='NewName')
print cloned_index_2.name

cloned_index_3 = index.copy(names=['NewName'])
print cloned_index_3.name

multi_index = pd.Index([(1, 2), (3, 4)], names=['MyName1', 'MyName2'])
print multi_index.names

multi_index_1 = multi_index.copy(names=['NewName1', 'NewName2'])
print multi_index_1.names
MyName
MyName
MyName
MyName
[u'MyName1', u'MyName2']
[u'MyName1', u'MyName2']
@chrisaycock
Copy link
Contributor

The user's name parameter is getting overwritten. Here's how to fix it in the code:

diff --git a/pandas/indexes/base.py b/pandas/indexes/base.py
index f430305..72c44ee 100644
--- a/pandas/indexes/base.py
+++ b/pandas/indexes/base.py
@@ -629,7 +629,7 @@ class Index(IndexOpsMixin, StringAccessorMixin, PandasObject):
             name = name or deepcopy(self.name)
         else:
             new_index = self._shallow_copy()
-            name = self.name
+            name = name or self.name
         if name is not None:
             names = [name]
         if names:

I need to add to the regression tests real quick.

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

Successfully merging a pull request may close this issue.

3 participants