Skip to content

API: Add DataFrame.droplevel #20342

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
TomAugspurger opened this issue Mar 14, 2018 · 7 comments · Fixed by #21871
Closed

API: Add DataFrame.droplevel #20342

TomAugspurger opened this issue Mar 14, 2018 · 7 comments · Fixed by #21871
Labels
API Design MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@TomAugspurger
Copy link
Contributor

As a way to make dropping an index level possible in an method chain.

>>> df = pd.DataFrame(np.arange(12).reshape(4, 3), columns=['a', 'b', 'c']).set_index(['a', 'b'])
>>> df.droplevel(0)

roughly equivalent to

df2 = df.copy()
df2.index = df.index.droplevel(0)
df2

cc @twiecki

@TomAugspurger TomAugspurger added Reshaping Concat, Merge/Join, Stack/Unstack, Explode API Design MultiIndex labels Mar 14, 2018
@ericmjl
Copy link

ericmjl commented Mar 14, 2018

@woohoo! I'd love for this to come into pandas, so that I don't have to worry about implementing it in pyjanitor! (Issue on my own package, which I'm more than willing to close if this happens: pyjanitor-devs/pyjanitor#8)

@ericmjl
Copy link

ericmjl commented Mar 14, 2018

@twiecki
Copy link
Contributor

twiecki commented Mar 14, 2018

Thanks @TomAugspurger! This would be a great addition.

One could make the same case for all other Index methods that return an index besides rename_axis and droplevel(), too. I wonder if it's worth thinking about a more general solution. E.g. something along the lines of a forward of df.index_*() -> df.index = df.index.*().

@jschendel
Copy link
Member

jschendel commented Mar 14, 2018

Not against this, but will it have any different functionality than reset_index with drop=True?

Seems like that covers what you'd want to do, unless I'm misinterpreting what this is supposed to do:

In [2]: df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=list('abcd')).set_index(list('abc'))

In [3]: df
Out[3]:
           d
a  b  c
0  1  2    3
4  5  6    7
8  9  10  11
12 13 14  15

In [4]: df.reset_index(level=1, drop=True)
Out[4]:
        d
a  c
0  2    3
4  6    7
8  10  11
12 14  15

In [5]: df.reset_index(level='c', drop=True)
Out[5]:
        d
a  b
0  1    3
4  5    7
8  9   11
12 13  15

In [6]: df.reset_index(level=['a', 'c'], drop=True)
Out[6]:
     d
b
1    3
5    7
9   11
13  15

@TomAugspurger
Copy link
Contributor Author

TomAugspurger commented Mar 14, 2018 via email

@tliu30
Copy link

tliu30 commented Jun 9, 2018

Don't think it totally covers everything...sometimes find a need to drop a column-index level, and i don't think reset_index supports columns (for good reason)

@jreback jreback added this to the 0.24.0 milestone Jul 12, 2018
@TomAugspurger
Copy link
Contributor Author

@jschendel had a good point that this will overlap with reset_index with drop=True. The advantages of a dedicated droplevel being

  • droplevel is just for dropping a level, whereas reset_index can drop or insert
  • droplevel can drop on either axis.

I don't have a strong opinion on whether we should add it, or better document reset_index.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants