Skip to content

Commit 525e48f

Browse files
lukemanleypmhatre1
authored andcommitted
REGR: DataFrame.sort_index not producing stable sort (pandas-dev#57169)
DataFrame.sort_index not producing stable sort
1 parent 88c4f93 commit 525e48f

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Fixed regressions
1818
- Fixed regression in :func:`merge_ordered` raising ``TypeError`` for ``fill_method="ffill"`` and ``how="left"`` (:issue:`57010`)
1919
- Fixed regression in :func:`wide_to_long` raising an ``AttributeError`` for string columns (:issue:`57066`)
2020
- Fixed regression in :meth:`DataFrame.loc` raising ``IndexError`` for non-unique, masked dtype indexes where result has more than 10,000 rows (:issue:`57027`)
21+
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
2122
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
2223
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
2324
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)

pandas/core/indexes/base.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -5933,17 +5933,14 @@ def sort_values(
59335933
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
59345934
"""
59355935
if key is None and (
5936-
self.is_monotonic_increasing or self.is_monotonic_decreasing
5936+
(ascending and self.is_monotonic_increasing)
5937+
or (not ascending and self.is_monotonic_decreasing)
59375938
):
5938-
reverse = ascending != self.is_monotonic_increasing
5939-
sorted_index = self[::-1] if reverse else self.copy()
59405939
if return_indexer:
59415940
indexer = np.arange(len(self), dtype=np.intp)
5942-
if reverse:
5943-
indexer = indexer[::-1]
5944-
return sorted_index, indexer
5941+
return self.copy(), indexer
59455942
else:
5946-
return sorted_index
5943+
return self.copy()
59475944

59485945
# GH 35584. Sort missing values according to na_position kwarg
59495946
# ignore na_position for MultiIndex

pandas/tests/frame/methods/test_sort_index.py

+24
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,27 @@ def test_axis_columns_ignore_index():
10011001
result = df.sort_index(axis="columns", ignore_index=True)
10021002
expected = DataFrame([[2, 1]])
10031003
tm.assert_frame_equal(result, expected)
1004+
1005+
1006+
def test_sort_index_stable_sort():
1007+
# GH 57151
1008+
df = DataFrame(
1009+
data=[
1010+
(Timestamp("2024-01-30 13:00:00"), 13.0),
1011+
(Timestamp("2024-01-30 13:00:00"), 13.1),
1012+
(Timestamp("2024-01-30 12:00:00"), 12.0),
1013+
(Timestamp("2024-01-30 12:00:00"), 12.1),
1014+
],
1015+
columns=["dt", "value"],
1016+
).set_index(["dt"])
1017+
result = df.sort_index(level="dt", kind="stable")
1018+
expected = DataFrame(
1019+
data=[
1020+
(Timestamp("2024-01-30 12:00:00"), 12.0),
1021+
(Timestamp("2024-01-30 12:00:00"), 12.1),
1022+
(Timestamp("2024-01-30 13:00:00"), 13.0),
1023+
(Timestamp("2024-01-30 13:00:00"), 13.1),
1024+
],
1025+
columns=["dt", "value"],
1026+
).set_index(["dt"])
1027+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)