Skip to content
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
2 changes: 1 addition & 1 deletion pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ def drop(self, labels, level=None, errors='raise'):
elif is_bool_indexer(loc):
if self.lexsort_depth == 0:
warnings.warn('dropping on a non-lexsorted multi-index'
'without a level parameter may impact '
' without a level parameter may impact '
'performance.',
PerformanceWarning,
stacklevel=2)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ def test_drop(self):
df.drop(labels=df[df.b > 0].index, inplace=True)
assert_frame_equal(df, expected)

def test_drop_multiindex_not_lexsorted(self):
# GH 11640

# define the lexsorted version
lexsorted_mi = MultiIndex.from_tuples(
[('a', ''), ('b1', 'c1'), ('b2', 'c2')], names=['b', 'c'])
lexsorted_df = DataFrame([[1, 3, 4]], columns=lexsorted_mi)
self.assertTrue(lexsorted_df.columns.is_lexsorted())

# define the non-lexsorted version
not_lexsorted_df = DataFrame(columns=['a', 'b', 'c', 'd'],
data=[[1, 'b1', 'c1', 3],
[1, 'b2', 'c2', 4]])
not_lexsorted_df = not_lexsorted_df.pivot_table(
index='a', columns=['b', 'c'], values='d')
not_lexsorted_df = not_lexsorted_df.reset_index()
self.assertFalse(not_lexsorted_df.columns.is_lexsorted())

# compare the results
tm.assert_frame_equal(lexsorted_df, not_lexsorted_df)
tm.assert_frame_equal(lexsorted_df.drop('a', axis=1),
not_lexsorted_df.drop('a', axis=1))

def test_reindex(self):
newFrame = self.frame.reindex(self.ts1.index)

Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -4198,6 +4198,29 @@ def test_groupby_multiindex_missing_pair(self):

tm.assert_frame_equal(res, exp)

def test_groupby_multiindex_not_lexsorted(self):
# GH 11640

# define the lexsorted version
lexsorted_mi = MultiIndex.from_tuples(
[('a', ''), ('b1', 'c1'), ('b2', 'c2')], names=['b', 'c'])
lexsorted_df = DataFrame([[1, 3, 4]], columns=lexsorted_mi)
self.assertTrue(lexsorted_df.columns.is_lexsorted())

# define the non-lexsorted version
not_lexsorted_df = DataFrame(columns=['a', 'b', 'c', 'd'],
data=[[1, 'b1', 'c1', 3],
[1, 'b2', 'c2', 4]])
not_lexsorted_df = not_lexsorted_df.pivot_table(
index='a', columns=['b', 'c'], values='d')
not_lexsorted_df = not_lexsorted_df.reset_index()
self.assertFalse(not_lexsorted_df.columns.is_lexsorted())

# compare the results
tm.assert_frame_equal(lexsorted_df, not_lexsorted_df)
tm.assert_frame_equal(lexsorted_df.groupby('a').mean(),
not_lexsorted_df.groupby('a').mean())

def test_groupby_levels_and_columns(self):
# GH9344, GH9049
idx_names = ['x', 'y']
Expand Down