File tree Expand file tree Collapse file tree 1 file changed +14
-5
lines changed Expand file tree Collapse file tree 1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -111,13 +111,20 @@ def compute(self, method: str) -> Series:
111
111
if n <= 0 :
112
112
return self .obj [[]]
113
113
114
- dropped = self .obj .dropna ()
115
- nan_index = self .obj .drop (dropped .index )
114
+ # Save index and reset to default index to avoid performance impact
115
+ # from when index contains duplicates
116
+ original_index = self .obj .index
117
+ cur_series = self .obj .reset_index (drop = True )
118
+
119
+ dropped = cur_series .dropna ()
120
+ nan_index = cur_series .drop (dropped .index )
116
121
117
122
# slow method
118
- if n >= len (self . obj ):
123
+ if n >= len (cur_series ):
119
124
ascending = method == "nsmallest"
120
- return self .obj .sort_values (ascending = ascending ).head (n )
125
+ final_series = cur_series .sort_values (ascending = ascending , kind = "mergesort" ).head (n )
126
+ final_series .index = original_index .take (final_series .index )
127
+ return final_series
121
128
122
129
# fast method
123
130
new_dtype = dropped .dtype
@@ -173,7 +180,9 @@ def compute(self, method: str) -> Series:
173
180
# reverse indices
174
181
inds = narr - 1 - inds
175
182
176
- return concat ([dropped .iloc [inds ], nan_index ]).iloc [:findex ]
183
+ final_series = concat ([dropped .iloc [inds ], nan_index ]).iloc [:findex ]
184
+ final_series .index = original_index .take (final_series .index )
185
+ return final_series
177
186
178
187
179
188
class SelectNFrame (SelectN [DataFrame ]):
You can’t perform that action at this time.
0 commit comments