File tree 1 file changed +14
-3
lines changed
python/cudf/cudf/core/groupby
1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -802,10 +802,21 @@ def nth(self, n):
802
802
"""
803
803
Return the nth row from each group.
804
804
"""
805
- result = self .agg (lambda x : x .nth (n )).sort_index ()
806
- sizes = self .size ().sort_index ()
807
805
808
- return result [sizes > n ]
806
+ self .obj ["__groupbynth_order__" ] = range (0 , len (self .obj ))
807
+ # We perform another groupby here to have the grouping columns
808
+ # be a part of dataframe columns.
809
+ result = self .obj .groupby (self .grouping .keys ).agg (lambda x : x .nth (n ))
810
+ sizes = self .size ().reindex (result .index )
811
+
812
+ result = result [sizes > n ]
813
+
814
+ result ._index = self .obj .index .take (
815
+ result ._data ["__groupbynth_order__" ]
816
+ )
817
+ del result ._data ["__groupbynth_order__" ]
818
+ del self .obj ._data ["__groupbynth_order__" ]
819
+ return result
809
820
810
821
@_cudf_nvtx_annotate
811
822
def ngroup (self , ascending = True ):
You can’t perform that action at this time.
0 commit comments