Skip to content

Commit

Permalink
attempt to mimic previous count_values behavior by reversing before sort
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKirko committed Oct 21, 2020
1 parent 7805d1e commit 2b75f78
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2b75f78

Please sign in to comment.