Skip to content

Commit e1fdbc0

Browse files
KristianHolsheimertm9k1
authored andcommitted
BUG: Fixed nlargest/smallest functionality for dataframes with MultiIndex columns (pandas-dev#23034)
1 parent 614e596 commit e1fdbc0

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

doc/source/whatsnew/v0.24.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,7 @@ Reshaping
11961196
- Bug in :func:`merge_asof` when merging on float values within defined tolerance (:issue:`22981`)
11971197
- Bug in :func:`pandas.concat` when concatenating a multicolumn DataFrame with tz-aware data against a DataFrame with a different number of columns (:issue`22796`)
11981198
- Bug in :func:`merge_asof` where confusing error message raised when attempting to merge with missing values (:issue:`23189`)
1199+
- Bug in :meth:`DataFrame.nsmallest` and :meth:`DataFrame.nlargest` for dataframes that have :class:`MultiIndex`ed columns (:issue:`23033`).
11991200

12001201
.. _whatsnew_0240.bug_fixes.sparse:
12011202

pandas/core/algorithms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1161,7 +1161,7 @@ class SelectNFrame(SelectN):
11611161

11621162
def __init__(self, obj, n, keep, columns):
11631163
super(SelectNFrame, self).__init__(obj, n, keep)
1164-
if not is_list_like(columns):
1164+
if not is_list_like(columns) or isinstance(columns, tuple):
11651165
columns = [columns]
11661166
columns = list(columns)
11671167
self.columns = columns

pandas/tests/frame/test_analytics.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -2153,7 +2153,7 @@ def test_n(self, df_strings, nselect_method, n, order):
21532153
tm.assert_frame_equal(result, expected)
21542154

21552155
@pytest.mark.parametrize('columns', [
2156-
('group', 'category_string'), ('group', 'string')])
2156+
['group', 'category_string'], ['group', 'string']])
21572157
def test_n_error(self, df_main_dtypes, nselect_method, columns):
21582158
df = df_main_dtypes
21592159
col = columns[1]
@@ -2259,3 +2259,20 @@ def test_series_nat_conversion(self):
22592259
df.rank()
22602260
result = df
22612261
tm.assert_frame_equal(result, expected)
2262+
2263+
def test_multiindex_column_lookup(self):
2264+
# Check whether tuples are correctly treated as multi-level lookups.
2265+
# GH 23033
2266+
df = pd.DataFrame(
2267+
columns=pd.MultiIndex.from_product([['x'], ['a', 'b']]),
2268+
data=[[0.33, 0.13], [0.86, 0.25], [0.25, 0.70], [0.85, 0.91]])
2269+
2270+
# nsmallest
2271+
result = df.nsmallest(3, ('x', 'a'))
2272+
expected = df.iloc[[2, 0, 3]]
2273+
tm.assert_frame_equal(result, expected)
2274+
2275+
# nlargest
2276+
result = df.nlargest(3, ('x', 'b'))
2277+
expected = df.iloc[[3, 2, 1]]
2278+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)