Skip to content
Merged
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ Reshaping
- Bug in :func:`merge_asof` when merging on float values within defined tolerance (:issue:`22981`)
- 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`)
- Bug in :func:`merge_asof` where confusing error message raised when attempting to merge with missing values (:issue:`23189`)
- Bug in :meth:`DataFrame.nsmallest` and :meth:`DataFrame.nlargest` for dataframes that have :class:`MultiIndex`ed columns (:issue:`23033`).

.. _whatsnew_0240.bug_fixes.sparse:

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ class SelectNFrame(SelectN):

def __init__(self, obj, n, keep, columns):
super(SelectNFrame, self).__init__(obj, n, keep)
if not is_list_like(columns):
if not is_list_like(columns) or isinstance(columns, tuple):
columns = [columns]
columns = list(columns)
self.columns = columns
Expand Down
19 changes: 18 additions & 1 deletion pandas/tests/frame/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,7 @@ def test_n(self, df_strings, nselect_method, n, order):
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize('columns', [
('group', 'category_string'), ('group', 'string')])
['group', 'category_string'], ['group', 'string']])
def test_n_error(self, df_main_dtypes, nselect_method, columns):
df = df_main_dtypes
col = columns[1]
Expand Down Expand Up @@ -2259,3 +2259,20 @@ def test_series_nat_conversion(self):
df.rank()
result = df
tm.assert_frame_equal(result, expected)

def test_multiindex_column_lookup(self):
# Check whether tuples are correctly treated as multi-level lookups.
# GH 23033
df = pd.DataFrame(
columns=pd.MultiIndex.from_product([['x'], ['a', 'b']]),
data=[[0.33, 0.13], [0.86, 0.25], [0.25, 0.70], [0.85, 0.91]])

# nsmallest
result = df.nsmallest(3, ('x', 'a'))
expected = df.iloc[[2, 0, 3]]
tm.assert_frame_equal(result, expected)

# nlargest
result = df.nlargest(3, ('x', 'b'))
expected = df.iloc[[3, 2, 1]]
tm.assert_frame_equal(result, expected)