-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: Add DataFrame.droplevel #21871
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 #21871
Changes from 3 commits
b46d46f
8836d3f
7862326
c43d261
95336db
5f9c648
808bc16
dbc25da
2333f7c
6b8f22b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4630,6 +4630,63 @@ def sortlevel(self, level=0, axis=0, ascending=True, inplace=False, | |
return self.sort_index(level=level, axis=axis, ascending=ascending, | ||
inplace=inplace, sort_remaining=sort_remaining) | ||
|
||
def droplevel(self, level, axis=0): | ||
"""Return DataFrame with requested index / column level(s) removed. | ||
|
||
Parameters | ||
---------- | ||
level : int, str, or list-like | ||
If a string is given, must be the name of a level | ||
If list-like, elements must be names or indexes of levels. | ||
|
||
axis : {0 or 'index', 1 or 'columns'}, default 0 | ||
|
||
.. versionadded:: 0.24.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move the versionadded to right after the summary (2nd line of doc-string) |
||
|
||
Returns | ||
------- | ||
DataFrame.droplevel() | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame([ | ||
...: [1, 2, 3, 4], | ||
...: [5, 6, 7, 8], | ||
...: [9, 10, 11, 12] | ||
...: ]).set_index([0, 1]).rename_axis(['a', 'b']) | ||
>>> df.columns = pd.MultiIndex.from_tuples([ | ||
...: ('c', 'e'), ('d', 'f') | ||
...:], names=['level_1', 'level_2']) | ||
>>> df | ||
level_1 c d | ||
level_2 e f | ||
a b | ||
1 2 3 4 | ||
5 6 7 8 | ||
9 10 11 12 | ||
>>> df.droplevel('a') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line between cases |
||
level_1 c d | ||
level_2 e f | ||
b | ||
2 3 4 | ||
6 7 8 | ||
10 11 12 | ||
>>> df.droplevel('level2', axis=1) | ||
level_1 c d | ||
a b | ||
1 2 3 4 | ||
5 6 7 8 | ||
9 10 11 12 | ||
|
||
""" | ||
result = self.copy() | ||
axis = self._get_axis_number(axis) | ||
if axis == 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be more generic (so works for series as well), eg. can remove the .copy() (as set_axis copies)
|
||
result.index = result.index.droplevel(level) | ||
else: | ||
result.columns = result.columns.droplevel(level) | ||
return result | ||
|
||
def nlargest(self, n, columns, keep='first'): | ||
""" | ||
Return the first `n` rows ordered by `columns` in descending order. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1056,6 +1056,30 @@ def test_reindex_signature(self): | |
"limit", "copy", "level", "method", | ||
"fill_value", "tolerance"} | ||
|
||
def test_droplevel(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the issue number as a comment |
||
df = pd.DataFrame([ | ||
[1, 2, 3, 4], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add tests for series as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did, on lines 1085-1089 :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh not in THIS test. pls separate out and put in tests/series/test_alter_axes |
||
[5, 6, 7, 8], | ||
[9, 10, 11, 12] | ||
]) | ||
df = df.set_index([0, 1]).rename_axis(['a', 'b']) | ||
df.columns = pd.MultiIndex.from_tuples([('c', 'e'), ('d', 'f')], | ||
names=['level_1', 'level_2']) | ||
|
||
# test that dropping of a level in the index works | ||
expected_df_no_level_a_in_index = df.reset_index('a', drop=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont' use these long names, expected and result are much easier to grok |
||
actual_df_no_level_a_in_index = df.droplevel('a') | ||
assert_frame_equal(expected_df_no_level_a_in_index, | ||
actual_df_no_level_a_in_index) | ||
|
||
# test that dropping of a level in the index works | ||
expected_df_no_level_2_in_columns = df.copy() | ||
expected_df_no_level_2_in_columns.columns = pd.Index(['c', 'd'], | ||
name='level_1') | ||
actual_df_no_level_2_in_columns = df.droplevel('level_2', axis=1) | ||
assert_frame_equal(expected_df_no_level_2_in_columns, | ||
actual_df_no_level_2_in_columns) | ||
|
||
|
||
class TestIntervalIndex(object): | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move this to generic so Series gets this as well