diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index ac5ad12a992b7..42396d920c3bd 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -437,6 +437,7 @@ Timezones Numeric ^^^^^^^ - Bug in :meth:`DataFrame.quantile`, :meth:`DataFrame.sort_values` causing incorrect subsequent indexing behavior (:issue:`38351`) +- Bug in :meth:`DataFrame.sort_values` raising an :class:`IndexError` for empty ``by`` (:issue:`40258`) - Bug in :meth:`DataFrame.select_dtypes` with ``include=np.number`` now retains numeric ``ExtensionDtype`` columns (:issue:`35340`) - Bug in :meth:`DataFrame.mode` and :meth:`Series.mode` not keeping consistent integer :class:`Index` for empty input (:issue:`33321`) - Bug in :meth:`DataFrame.rank` with ``np.inf`` and mixture of ``np.nan`` and ``np.inf`` (:issue:`32593`) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index dbf2446f43af3..64759dcf07d8b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5818,7 +5818,7 @@ def sort_values( # type: ignore[override] keys, orders=ascending, na_position=na_position, key=key ) indexer = ensure_platform_int(indexer) - else: + elif len(by): by = by[0] k = self._get_label_or_level_values(by, axis=axis) @@ -5833,6 +5833,8 @@ def sort_values( # type: ignore[override] indexer = nargsort( k, kind=kind, ascending=ascending, na_position=na_position, key=key ) + else: + return self.copy() new_data = self._mgr.take( indexer, axis=self._get_block_manager_axis(axis), verify=False diff --git a/pandas/tests/frame/methods/test_sort_values.py b/pandas/tests/frame/methods/test_sort_values.py index eaea22df3adfe..2ca5f6aa72241 100644 --- a/pandas/tests/frame/methods/test_sort_values.py +++ b/pandas/tests/frame/methods/test_sort_values.py @@ -79,6 +79,13 @@ def test_sort_values(self): with pytest.raises(ValueError, match=msg): frame.sort_values(by=["A", "B"], axis=0, ascending=[True] * 5) + def test_sort_values_by_empty_list(self): + # https://github.com/pandas-dev/pandas/issues/40258 + expected = DataFrame({"a": [1, 4, 2, 5, 3, 6]}) + result = expected.sort_values(by=[]) + tm.assert_frame_equal(result, expected) + assert result is not expected + def test_sort_values_inplace(self): frame = DataFrame( np.random.randn(4, 4), index=[1, 2, 3, 4], columns=["A", "B", "C", "D"]