Skip to content

BUG: reset_index can reset index name #17067

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
p-himik opened this issue Jul 24, 2017 · 9 comments · Fixed by #23116
Closed

BUG: reset_index can reset index name #17067

p-himik opened this issue Jul 24, 2017 · 9 comments · Fixed by #23116
Labels
Bug good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@p-himik
Copy link

p-himik commented Jul 24, 2017

import pandas as pd
assert (pd.DataFrame(index=pd.Index([], name='x'))
        .reset_index(level=[]).index.name) is None

The root of the problem is in pandas.core.common._default_index:

def _default_index(n):
    from pandas.core.index import RangeIndex
    return RangeIndex(0, n, name=None)

Probably, a new optional argument name can be introduced to _default_index and used in reset_index.

@gfyoung gfyoung added Indexing Related to indexing on series/frames, not to indexes themselves Bug labels Jul 24, 2017
@gfyoung
Copy link
Member

gfyoung commented Jul 24, 2017

@p-himik : Thanks for the issue! Indeed this is a bug, for the documentation says the following:

"For a standard index, the index name will be used (if set), otherwise a default ‘index’ or ‘level_0’ (if ‘index’ is already taken) will be used."

Here's a similar bug that should also be pointed out:

s = pd.Series([1, 2, 3])
s.index.name = "foo"

s.index += 1
assert s.index.name == "foo"

The index name magically disappears like in your example (probably linked to the code you pointed out). PR is welcome to patch these issues!

@gfyoung gfyoung changed the title reset_index can reset index name BUG: reset_index can reset index name Jul 24, 2017
@hurcy
Copy link

hurcy commented Aug 14, 2017

I would like to working on this issue.

@gfyoung
Copy link
Member

gfyoung commented Aug 14, 2017

@hurcy : By all means! You can read the documentation here about contributing. Let us know if you have any questions about the code or contributing.

@jreback
Copy link
Contributor

jreback commented Aug 18, 2017

In [16]: pd.DataFrame(index=pd.Index([], name='x')).reset_index(level=[]).index.name is None
Out[16]: True

In [17]: pd.__version__
Out[17]: '0.21.0.dev+371.g0ee1675'

The original assertion is already true in master. I think the issue is that level=[] is actually an invalid specification.

@chris-b1 @jorisvandenbossche

@hurcy
Copy link

hurcy commented Aug 18, 2017 via email

@jreback jreback modified the milestones: 0.21.0, Next Major Release Oct 2, 2017
@minggli
Copy link
Contributor

minggli commented Oct 12, 2018

Hello, is this issue still open for a fix? IMHO, reset_index without an index name is not necessarily a bad thing. Do we actually want to keep index name during reset_index()?

@minggli
Copy link
Contributor

minggli commented Oct 14, 2018

it appears this issue has already been fixed. perhaps can be closed?

@jorisvandenbossche jorisvandenbossche added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Effort Low labels Oct 15, 2018
@jorisvandenbossche
Copy link
Member

It would be good to still add a test to assert this keeps working

@gfyoung
Copy link
Member

gfyoung commented Oct 15, 2018

It would be good to still add a test to assert this keeps working

@jorisvandenbossche : Indeed! That's what @minggli is trying to do in the now-reopened #23116.

@jreback jreback modified the milestones: Contributions Welcome, 0.24.0 Oct 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
6 participants