Skip to content

Commit 2b75f78

Browse files
committed
attempt to mimic previous count_values behavior by reversing before sort
1 parent 7805d1e commit 2b75f78

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

pandas/core/algorithms.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -819,22 +819,19 @@ def value_counts_arraylike(values, dropna: bool):
819819
# TODO: handle uint8
820820
f = getattr(htable, f"value_count_{ndtype}")
821821
keys, counts = f(values, dropna)
822+
# GH 35922. Mimic previous value_counts behavior now that
823+
# Series.sort_values is stable
824+
keys = keys[::-1]
825+
counts = counts[::-1]
822826

823827
mask = isna(values)
824828
if not dropna and mask.any():
825829
# GH 35922. Series.sort_values is stable now, so need to
826-
# append NaN counts or move to the end to make sure they are
830+
# append NaN counts to make sure they are
827831
# sorted toward the end when calling value_counts
828832
if not isna(keys).any():
829833
keys = np.append(keys, np.NaN)
830834
counts = np.append(counts, mask.sum())
831-
else:
832-
nan_pos = np.where(np.isnan(keys))
833-
keys[nan_pos] = keys[-1]
834-
keys[-1] = np.NaN
835-
tmp = counts[nan_pos]
836-
counts[nan_pos] = counts[-1]
837-
counts[-1] = tmp
838835

839836
keys = _reconstruct_data(keys, original.dtype, original)
840837

0 commit comments

Comments
 (0)