Skip to content

Keep name after DataFrame drop (issue 2939) #2961

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 7 additions & 2 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def drop(self, labels, axis=0, level=None):
dropped : type of caller
"""
axis_name = self._get_axis_name(axis)
axis = self._get_axis(axis)
axis, axis_ = self._get_axis(axis), axis

if axis.is_unique:
if level is not None:
Expand All @@ -349,8 +349,13 @@ def drop(self, labels, axis=0, level=None):
new_axis = axis.drop(labels, level=level)
else:
new_axis = axis.drop(labels)
dropped = self.reindex(**{axis_name: new_axis})
try:
dropped.axes[axis_].names = axis.names
except AttributeError:
pass
return dropped

return self.reindex(**{axis_name: new_axis})
else:
if level is not None:
if not isinstance(axis, MultiIndex):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5103,6 +5103,16 @@ def test_corrwith_series(self):

assert_series_equal(result, expected)

def test_drop_names(self):
df = DataFrame([[1, 2, 3],[3, 4, 5],[5, 6, 7]], index=['a', 'b', 'c'], columns=['d', 'e', 'f'])
df.index.name, df.columns.name = 'first', 'second'
df_dropped_b = df.drop('b')
df_dropped_e = df.drop('e', axis=1)
self.assert_(df_dropped_b.index.name == 'first')
self.assert_(df_dropped_e.index.name == 'first')
self.assert_(df_dropped_b.columns.name == 'second')
self.assert_(df_dropped_e.columns.name == 'second')

def test_dropEmptyRows(self):
N = len(self.frame.index)
mat = randn(N)
Expand Down
2 changes: 2 additions & 0 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,12 @@ def assert_frame_equal(left, right, check_dtype=True,
assert(type(left.index) == type(right.index))
assert(left.index.dtype == right.index.dtype)
assert(left.index.inferred_type == right.index.inferred_type)
assert(left.index.names == right.index.names)
if check_column_type:
assert(type(left.columns) == type(right.columns))
assert(left.columns.dtype == right.columns.dtype)
assert(left.columns.inferred_type == right.columns.inferred_type)
assert(left.columns.names == right.columns.names)


def assert_panel_equal(left, right,
Expand Down