Skip to content

Commit 9b51d42

Browse files
committed
mv NaNs to the end of dupe lists in value_counts
1 parent 12eb535 commit 9b51d42

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

pandas/core/algorithms.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,19 @@ def value_counts_arraylike(values, dropna: bool):
822822

823823
mask = isna(values)
824824
if not dropna and mask.any():
825+
# 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
827+
# sorted toward the end when calling value_counts
825828
if not isna(keys).any():
826-
keys = np.insert(keys, 0, np.NaN)
827-
counts = np.insert(counts, 0, mask.sum())
829+
keys = np.append(keys, np.NaN)
830+
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
828838

829839
keys = _reconstruct_data(keys, original.dtype, original)
830840

0 commit comments

Comments
 (0)