Skip to content
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

BUG: index.map() deletes index.name #14500

Closed
Khris777 opened this issue Oct 26, 2016 · 3 comments
Closed

BUG: index.map() deletes index.name #14500

Khris777 opened this issue Oct 26, 2016 · 3 comments
Labels
Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@Khris777
Copy link

Khris777 commented Oct 26, 2016

This is related to #12766
Possibly a duplicate.

A small, complete example of the issue

DD = pd.DataFrame({'A':[1,2,3,4,5]})
DD.index.name = 'Test'
print DD.index.name
DD.index = DD.index.map(unicode)
print DD.index.name

Expected Output

The mapping operation deletes the index name.

However mapping a function on the index means modifying an existing index, so one would expect the name of the index to stay the same as opposed to when choosing a completely new index.

Output of pd.show_versions()

python: 2.7.12.final.0
pandas: 0.19.0

EDIT:

Doing a set_index(column) does NOT set the column name as the index name, I think it would be common sense that the name of the column which becomes the new index be automatically set as the index name.

@jorisvandenbossche
Copy link
Member

This is indeed because of #12766. The Index.map returns an array, which does not hold the index name. So setting the index to that array, will result in an index without name.
The fix here is to have Index.map return an Index, which can keep the original name -> #12766

@jorisvandenbossche
Copy link
Member

Regarding your other question, set_index does use the column name for the index name:

In [43]: df = pd.DataFrame({'a':[1,2,3], 'b': [4,5,6]})

In [44]: df
Out[44]: 
   a  b
0  1  4
1  2  5
2  3  6

In [45]: df.set_index('a')
Out[45]: 
   b
a   
1  4
2  5
3  6

In [46]: df.set_index('a').index.name
Out[46]: 'a'

In [47]: df.set_index(df['a']).index.name
Out[47]: 'a'

Do you have an example where this is not the case?

@jorisvandenbossche jorisvandenbossche added the Indexing Related to indexing on series/frames, not to indexes themselves label Oct 26, 2016
@jorisvandenbossche jorisvandenbossche added this to the No action milestone Oct 26, 2016
@jorisvandenbossche
Copy link
Member

Going to close this one, the actual issue will be solved once #12766 is fixed

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants