From 2b75f789d18840ec50d33eb83dae963e7f303c8c Mon Sep 17 00:00:00 2001 From: Alexander Kirko Date: Wed, 21 Oct 2020 16:59:54 +0300 Subject: [PATCH] attempt to mimic previous count_values behavior by reversing before sort --- pandas/core/algorithms.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index caba3245c773a..98309f6e2b891 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -819,22 +819,19 @@ def value_counts_arraylike(values, dropna: bool): # TODO: handle uint8 f = getattr(htable, f"value_count_{ndtype}") keys, counts = f(values, dropna) + # GH 35922. Mimic previous value_counts behavior now that + # Series.sort_values is stable + keys = keys[::-1] + counts = counts[::-1] mask = isna(values) if not dropna and mask.any(): # GH 35922. Series.sort_values is stable now, so need to - # append NaN counts or move to the end to make sure they are + # append NaN counts to make sure they are # sorted toward the end when calling value_counts if not isna(keys).any(): keys = np.append(keys, np.NaN) counts = np.append(counts, mask.sum()) - else: - nan_pos = np.where(np.isnan(keys)) - keys[nan_pos] = keys[-1] - keys[-1] = np.NaN - tmp = counts[nan_pos] - counts[nan_pos] = counts[-1] - counts[-1] = tmp keys = _reconstruct_data(keys, original.dtype, original)