Skip to content

Commit 2393a23

Browse files
authored
REGR: sort_index with ascending as list of boolean (#56049)
1 parent ab144c4 commit 2393a23

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

pandas/core/sorting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def get_indexer_indexer(
9898
sort_remaining=sort_remaining,
9999
na_position=na_position,
100100
)
101-
elif (ascending and target.is_monotonic_increasing) or (
102-
not ascending and target.is_monotonic_decreasing
101+
elif (np.all(ascending) and target.is_monotonic_increasing) or (
102+
not np.any(ascending) and target.is_monotonic_decreasing
103103
):
104104
# Check monotonic-ness before sort an index (GH 11080)
105105
return None

pandas/tests/series/methods/test_sort_index.py

+18
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,21 @@ def test_sort_values_key_type(self):
317317
result = s.sort_index(key=lambda x: x.month_name())
318318
expected = s.iloc[[2, 1, 0]]
319319
tm.assert_series_equal(result, expected)
320+
321+
@pytest.mark.parametrize(
322+
"ascending",
323+
[
324+
[True, False],
325+
[False, True],
326+
],
327+
)
328+
def test_sort_index_multi_already_monotonic(self, ascending):
329+
# GH 56049
330+
mi = MultiIndex.from_product([[1, 2], [3, 4]])
331+
ser = Series(range(len(mi)), index=mi)
332+
result = ser.sort_index(ascending=ascending)
333+
if ascending == [True, False]:
334+
expected = ser.take([1, 0, 3, 2])
335+
elif ascending == [False, True]:
336+
expected = ser.take([2, 3, 0, 1])
337+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)