Skip to content

Commit e33de24

Browse files
committed
#63155 stable sort value_counts
1 parent fcffde9 commit e33de24

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

pandas/core/algorithms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def value_counts_internal(
948948
result = Series(counts, index=idx, name=name, copy=False)
949949

950950
if sort:
951-
result = result.sort_values(ascending=ascending)
951+
result = result.sort_values(ascending=ascending, kind="stable")
952952

953953
if normalize:
954954
result = result / counts.sum()

pandas/core/base.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,12 @@ def value_counts(
993993
If True then the object returned will contain the relative
994994
frequencies of the unique values.
995995
sort : bool, default True
996-
Sort by frequencies when True. Preserve the order of the data when False.
996+
Stable sort by frequencies when True. Preserve the order of the data
997+
when False.
998+
999+
.. versionchanged:: 3.0.0
1000+
1001+
Prior to 3.0.0, the sort was unstable.
9971002
ascending : bool, default False
9981003
Sort in ascending order.
9991004
bins : int, optional

pandas/core/frame.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7718,11 +7718,16 @@ def value_counts(
77187718
normalize : bool, default False
77197719
Return proportions rather than frequencies.
77207720
sort : bool, default True
7721-
Sort by frequencies when True. Preserve the order of the data when False.
7721+
Stable sort by frequencies when True. Preserve the order of the data
7722+
when False.
77227723
77237724
.. versionchanged:: 3.0.0
77247725
77257726
Prior to 3.0.0, ``sort=False`` would sort by the columns values.
7727+
7728+
.. versionchanged:: 3.0.0
7729+
7730+
Prior to 3.0.0, the sort was unstable.
77267731
ascending : bool, default False
77277732
Sort in ascending order.
77287733
dropna : bool, default True
@@ -7832,7 +7837,7 @@ def value_counts(
78327837
counts.name = name
78337838

78347839
if sort:
7835-
counts = counts.sort_values(ascending=ascending)
7840+
counts = counts.sort_values(ascending=ascending, kind="stable")
78367841
if normalize:
78377842
counts /= counts.sum()
78387843

pandas/core/groupby/generic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,8 +2776,8 @@ def value_counts(
27762776
normalize : bool, default False
27772777
Return proportions rather than frequencies.
27782778
sort : bool, default True
2779-
Sort by frequencies when True. When False, non-grouping columns will appear
2780-
in the order they occur in within groups.
2779+
Stable sort by frequencies when True. When False, non-grouping
2780+
columns will appear in the order they occur in within groups.
27812781
27822782
.. versionchanged:: 3.0.0
27832783

0 commit comments

Comments
 (0)