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

API: Add DataFrame.droplevel #20342

Closed
TomAugspurger opened this issue Mar 14, 2018 · 7 comments

Comments

Projects
None yet
6 participants
@TomAugspurger
Copy link
Contributor

commented Mar 14, 2018

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

@ericmjl

This comment has been minimized.

Copy link

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: ericmjl/pyjanitor#8)

@twiecki

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Contributor Author

commented Mar 14, 2018

@tliu30

This comment has been minimized.

Copy link

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)

@vbarda vbarda referenced this issue Jul 12, 2018

Merged

API: Add DataFrame.droplevel #21871

4 of 4 tasks complete

@jreback jreback added this to the 0.24.0 milestone Jul 12, 2018

@TomAugspurger

This comment has been minimized.

Copy link
Contributor Author

commented Jul 12, 2018

@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
You can’t perform that action at this time.