Skip to content

ENH: name(s) argument for reset_index? #6878

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
jorisvandenbossche opened this issue Apr 13, 2014 · 6 comments · Fixed by #46167
Closed

ENH: name(s) argument for reset_index? #6878

jorisvandenbossche opened this issue Apr 13, 2014 · 6 comments · Fixed by #46167
Labels
Enhancement Index Related to the Index class or subclasses
Milestone

Comments

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Apr 13, 2014

An idea: to be able to specify the a name when resetting the index, which would override the index name or the default value when index name is None.

Eg something like this:

In [10]: df = pd.DataFrame(np.random.rand(4,3), columns=list('ABC'), index=pd.date_range('2013-01-01', periods=4))
In [11]: df
Out[11]: 
                   A         B         C
2013-01-01  0.313664  0.606445  0.048081
2013-01-02  0.907785  0.004429  0.374456
2013-01-03  0.916584  0.067639  0.467712
2013-01-04  0.712824  0.687895  0.397960

In [12]: df.reset_index()
Out[12]: 
       index         A         B         C
0 2013-01-01  0.313664  0.606445  0.048081
1 2013-01-02  0.907785  0.004429  0.374456
2 2013-01-03  0.916584  0.067639  0.467712
3 2013-01-04  0.712824  0.687895  0.397960

In [12]: df.reset_index(name='datetime')
Out[12]: 
    datetime         A         B         C
0 2013-01-01  0.313664  0.606445  0.048081
1 2013-01-02  0.907785  0.004429  0.374456
2 2013-01-03  0.916584  0.067639  0.467712
3 2013-01-04  0.712824  0.687895  0.397960

For multi-index, you should provide a list.

@jreback
Copy link
Contributor

jreback commented Apr 13, 2014

you can do

df = df.copy()
df.index = df.index.set_names([...])
df.reset_index()

so this is a convience yes?

@jorisvandenbossche
Copy link
Member Author

yes, as a convenience I think (and like this it would do a copy less, only the copy inside reset_index?)

@jreback
Copy link
Contributor

jreback commented Apr 13, 2014

Series already has this do just a extending to df yes?

sounds good

@jorisvandenbossche
Copy link
Member Author

Ah, I didn't know Series.reset_index already had something like this. Would certainly be a reason to get that aligned.

Only problems is that it is different there, as it specifies the name for the Series values, not for the index ..

In [19]: s = df['A']
In [20]: s.reset_index()
Out[20]: 
       index         A
0 2013-01-01  0.313664
1 2013-01-02  0.907785
2 2013-01-03  0.916584
3 2013-01-04  0.712824

In [21]: s.reset_index(name='other name')
Out[21]: 
       index  other name
0 2013-01-01    0.313664
1 2013-01-02    0.907785
2 2013-01-03    0.916584
3 2013-01-04    0.712824

@cpcloud
Copy link
Member

cpcloud commented Apr 14, 2014

index name kwarg?

@cancan101
Copy link
Contributor

This also partially solves #6181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Index Related to the Index class or subclasses
Projects
None yet
5 participants