File tree 1 file changed +12
-2
lines changed
1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -822,9 +822,19 @@ def value_counts_arraylike(values, dropna: bool):
822
822
823
823
mask = isna (values )
824
824
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
825
828
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
828
838
829
839
keys = _reconstruct_data (keys , original .dtype , original )
830
840
You can’t perform that action at this time.
0 commit comments