@@ -95,7 +95,7 @@ def _groupby_function(name, alias, npfunc, numeric_only=True,
95
95
@Appender (_doc_template )
96
96
@Appender (_local_template )
97
97
def f (self ):
98
- self ._set_selection_from_grouper ()
98
+ self ._set_group_selection ()
99
99
try :
100
100
return self ._cython_agg_general (alias , numeric_only = numeric_only )
101
101
except AssertionError as e :
@@ -457,13 +457,21 @@ def _selected_obj(self):
457
457
else :
458
458
return self .obj [self ._selection ]
459
459
460
- def _reset_group_selection (self ):
460
+ def _clear_group_selection (self ):
461
+ """
462
+ Clear group based selection. Used for methods needing to return info on
463
+ each group regardless of whether a group selection was previously set.
464
+ """
461
465
if self ._group_selection is not None :
462
466
self ._group_selection = None
467
+ # GH12839 clear cached selection too when changing group selection
463
468
self ._reset_cache ('_selected_obj' )
464
469
465
- def _set_selection_from_grouper (self ):
466
- """ we may need create a selection if we have non-level groupers """
470
+ def _set_group_selection (self ):
471
+ """
472
+ Create group based selection. Used when selection is not passed
473
+ directly but instead via a grouper.
474
+ """
467
475
grp = self .grouper
468
476
if self .as_index and getattr (grp , 'groupings' , None ) is not None and \
469
477
self .obj .ndim > 1 :
@@ -473,6 +481,7 @@ def _set_selection_from_grouper(self):
473
481
474
482
if len (groupers ):
475
483
self ._group_selection = ax .difference (Index (groupers )).tolist ()
484
+ # GH12839 clear selected obj cache when group selection changes
476
485
self ._reset_cache ('_selected_obj' )
477
486
478
487
def _set_result_index_ordered (self , result ):
@@ -517,7 +526,7 @@ def _make_wrapper(self, name):
517
526
518
527
# need to setup the selection
519
528
# as are not passed directly but in the grouper
520
- self ._set_selection_from_grouper ()
529
+ self ._set_group_selection ()
521
530
522
531
f = getattr (self ._selected_obj , name )
523
532
if not isinstance (f , types .MethodType ):
@@ -985,7 +994,7 @@ def mean(self, *args, **kwargs):
985
994
except GroupByError :
986
995
raise
987
996
except Exception : # pragma: no cover
988
- self ._set_selection_from_grouper ()
997
+ self ._set_group_selection ()
989
998
f = lambda x : x .mean (axis = self .axis )
990
999
return self ._python_agg_general (f )
991
1000
@@ -1003,7 +1012,7 @@ def median(self):
1003
1012
raise
1004
1013
except Exception : # pragma: no cover
1005
1014
1006
- self ._set_selection_from_grouper ()
1015
+ self ._set_group_selection ()
1007
1016
1008
1017
def f (x ):
1009
1018
if isinstance (x , np .ndarray ):
@@ -1046,7 +1055,7 @@ def var(self, ddof=1, *args, **kwargs):
1046
1055
if ddof == 1 :
1047
1056
return self ._cython_agg_general ('var' )
1048
1057
else :
1049
- self ._set_selection_from_grouper ()
1058
+ self ._set_group_selection ()
1050
1059
f = lambda x : x .var (ddof = ddof )
1051
1060
return self ._python_agg_general (f )
1052
1061
@@ -1222,7 +1231,7 @@ def nth(self, n, dropna=None):
1222
1231
raise TypeError ("n needs to be an int or a list/set/tuple of ints" )
1223
1232
1224
1233
nth_values = np .array (nth_values , dtype = np .intp )
1225
- self ._set_selection_from_grouper ()
1234
+ self ._set_group_selection ()
1226
1235
1227
1236
if not dropna :
1228
1237
mask = np .in1d (self ._cumcount_array (), nth_values ) | \
@@ -1330,7 +1339,7 @@ def cumcount(self, ascending=True):
1330
1339
dtype: int64
1331
1340
"""
1332
1341
1333
- self ._set_selection_from_grouper ()
1342
+ self ._set_group_selection ()
1334
1343
1335
1344
index = self ._selected_obj .index
1336
1345
cumcounts = self ._cumcount_array (ascending = ascending )
@@ -1408,7 +1417,7 @@ def head(self, n=5):
1408
1417
0 1 2
1409
1418
2 5 6
1410
1419
"""
1411
- self ._reset_group_selection ()
1420
+ self ._clear_group_selection ()
1412
1421
mask = self ._cumcount_array () < n
1413
1422
return self ._selected_obj [mask ]
1414
1423
@@ -1435,7 +1444,7 @@ def tail(self, n=5):
1435
1444
0 a 1
1436
1445
2 b 1
1437
1446
"""
1438
- self ._reset_group_selection ()
1447
+ self ._clear_group_selection ()
1439
1448
mask = self ._cumcount_array (ascending = False ) < n
1440
1449
return self ._selected_obj [mask ]
1441
1450
0 commit comments