@@ -475,6 +475,24 @@ def _set_selection_from_grouper(self):
475475 if len (groupers ):
476476 self ._group_selection = (ax - Index (groupers )).tolist ()
477477
478+ def _set_result_index_ordered (self , result ):
479+ # set the result index on the passed values object
480+ # return the new object
481+ # related 8046
482+
483+ # the values/counts are repeated according to the group index
484+ indices = self .indices
485+
486+ # shortcut of we have an already ordered grouper
487+
488+ if not Index (self .grouper .group_info [0 ]).is_monotonic :
489+ index = Index (np .concatenate ([ indices [v ] for v in self .grouper .result_index ]))
490+ result .index = index
491+ result = result .sort_index ()
492+
493+ result .index = self .obj .index
494+ return result
495+
478496 def _local_dir (self ):
479497 return sorted (set (self .obj ._local_dir () + list (self ._apply_whitelist )))
480498
@@ -2087,7 +2105,6 @@ def _convert_grouper(axis, grouper):
20872105 else :
20882106 return grouper
20892107
2090-
20912108class SeriesGroupBy (GroupBy ):
20922109 _apply_whitelist = _series_apply_whitelist
20932110
@@ -2319,18 +2336,7 @@ def _transform_fast(self, func):
23192336 counts = self .count ().values
23202337 values = np .repeat (values , com ._ensure_platform_int (counts ))
23212338
2322- # the values/counts are repeated according to the group index
2323- indices = self .indices
2324-
2325- # shortcut of we have an already ordered grouper
2326- if Index (self .grouper .group_info [0 ]).is_monotonic :
2327- result = Series (values , index = self .obj .index )
2328- else :
2329- index = Index (np .concatenate ([ indices [v ] for v in self .grouper .result_index ]))
2330- result = Series (values , index = index ).sort_index ()
2331- result .index = self .obj .index
2332-
2333- return result
2339+ return self ._set_result_index_ordered (Series (values ))
23342340
23352341 def filter (self , func , dropna = True , * args , ** kwargs ):
23362342 """
@@ -2842,8 +2848,7 @@ def _transform_general(self, func, *args, **kwargs):
28422848 concat_index = obj .columns if self .axis == 0 else obj .index
28432849 concatenated = concat (applied , join_axes = [concat_index ],
28442850 axis = self .axis , verify_integrity = False )
2845- concatenated .sort_index (inplace = True )
2846- return concatenated
2851+ return self ._set_result_index_ordered (concatenated )
28472852
28482853 def transform (self , func , * args , ** kwargs ):
28492854 """
0 commit comments