Skip to content

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

Merged
merged 10 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

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


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
Copy link
Contributor

Choose a reason for hiding this comment

The 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')
Copy link
Contributor

Choose a reason for hiding this comment

The 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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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)

labels = self._get_axis(axis)
labels = labels.droplevel(level)
result = self.set_axis(labels, axis=axis)
return result

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.
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,30 @@ def test_reindex_signature(self):
"limit", "copy", "level", "method",
"fill_value", "tolerance"}

def test_droplevel(self):
Copy link
Contributor

Choose a reason for hiding this comment

The 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],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add tests for series as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, on lines 1085-1089 :)

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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):

Expand Down