Skip to content

Commit 9760b64

Browse files
authored
REGR: DataFrame.sort_index not producing stable sort (#57169)
DataFrame.sort_index not producing stable sort
1 parent dc16177 commit 9760b64

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
@@ -5952,17 +5952,14 @@ def sort_values(
59525952
(Index([1000, 100, 10, 1], dtype='int64'), array([3, 1, 0, 2]))
59535953
"""
59545954
if key is None and (
5955-
self.is_monotonic_increasing or self.is_monotonic_decreasing
5955+
(ascending and self.is_monotonic_increasing)
5956+
or (not ascending and self.is_monotonic_decreasing)
59565957
):
5957-
reverse = ascending != self.is_monotonic_increasing
5958-
sorted_index = self[::-1] if reverse else self.copy()
59595958
if return_indexer:
59605959
indexer = np.arange(len(self), dtype=np.intp)
5961-
if reverse:
5962-
indexer = indexer[::-1]
5963-
return sorted_index, indexer
5960+
return self.copy(), indexer
59645961
else:
5965-
return sorted_index
5962+
return self.copy()
59665963

59675964
# GH 35584. Sort missing values according to na_position kwarg
59685965
# 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)