@@ -475,6 +475,24 @@ def _set_selection_from_grouper(self):
475
475
if len (groupers ):
476
476
self ._group_selection = (ax - Index (groupers )).tolist ()
477
477
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
+
478
496
def _local_dir (self ):
479
497
return sorted (set (self .obj ._local_dir () + list (self ._apply_whitelist )))
480
498
@@ -2087,7 +2105,6 @@ def _convert_grouper(axis, grouper):
2087
2105
else :
2088
2106
return grouper
2089
2107
2090
-
2091
2108
class SeriesGroupBy (GroupBy ):
2092
2109
_apply_whitelist = _series_apply_whitelist
2093
2110
@@ -2319,18 +2336,7 @@ def _transform_fast(self, func):
2319
2336
counts = self .count ().values
2320
2337
values = np .repeat (values , com ._ensure_platform_int (counts ))
2321
2338
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 ))
2334
2340
2335
2341
def filter (self , func , dropna = True , * args , ** kwargs ):
2336
2342
"""
@@ -2842,8 +2848,7 @@ def _transform_general(self, func, *args, **kwargs):
2842
2848
concat_index = obj .columns if self .axis == 0 else obj .index
2843
2849
concatenated = concat (applied , join_axes = [concat_index ],
2844
2850
axis = self .axis , verify_integrity = False )
2845
- concatenated .sort_index (inplace = True )
2846
- return concatenated
2851
+ return self ._set_result_index_ordered (concatenated )
2847
2852
2848
2853
def transform (self , func , * args , ** kwargs ):
2849
2854
"""
0 commit comments