@@ -546,18 +546,6 @@ def f(self):
546
546
return attr
547
547
548
548
549
- @contextmanager
550
- def group_selection_context (groupby : GroupBy ) -> Iterator [GroupBy ]:
551
- """
552
- Set / reset the group_selection_context.
553
- """
554
- groupby ._set_group_selection ()
555
- try :
556
- yield groupby
557
- finally :
558
- groupby ._reset_group_selection ()
559
-
560
-
561
549
_KeysArgType = Union [
562
550
Hashable ,
563
551
List [Hashable ],
@@ -915,7 +903,7 @@ def __getattr__(self, attr: str):
915
903
def _make_wrapper (self , name : str ) -> Callable :
916
904
assert name in self ._apply_allowlist
917
905
918
- with group_selection_context ( self ):
906
+ with self . _group_selection_context ( ):
919
907
# need to setup the selection
920
908
# as are not passed directly but in the grouper
921
909
f = getattr (self ._obj_with_exclusions , name )
@@ -992,6 +980,17 @@ def _reset_group_selection(self) -> None:
992
980
self ._group_selection = None
993
981
self ._reset_cache ("_selected_obj" )
994
982
983
+ @contextmanager
984
+ def _group_selection_context (self ) -> Iterator [GroupBy ]:
985
+ """
986
+ Set / reset the _group_selection_context.
987
+ """
988
+ self ._set_group_selection ()
989
+ try :
990
+ yield self
991
+ finally :
992
+ self ._reset_group_selection ()
993
+
995
994
def _iterate_slices (self ) -> Iterable [Series ]:
996
995
raise AbstractMethodError (self )
997
996
@@ -1365,7 +1364,7 @@ def f(g):
1365
1364
# fails on *some* columns, e.g. a numeric operation
1366
1365
# on a string grouper column
1367
1366
1368
- with group_selection_context ( self ):
1367
+ with self . _group_selection_context ( ):
1369
1368
return self ._python_apply_general (f , self ._selected_obj )
1370
1369
1371
1370
return result
@@ -1445,7 +1444,7 @@ def _agg_general(
1445
1444
npfunc : Callable ,
1446
1445
):
1447
1446
1448
- with group_selection_context ( self ):
1447
+ with self . _group_selection_context ( ):
1449
1448
# try a cython aggregation if we can
1450
1449
result = self ._cython_agg_general (
1451
1450
how = alias ,
@@ -1552,7 +1551,7 @@ def _transform(self, func, *args, engine=None, engine_kwargs=None, **kwargs):
1552
1551
1553
1552
if maybe_use_numba (engine ):
1554
1553
# TODO: tests with self._selected_obj.ndim == 1 on DataFrameGroupBy
1555
- with group_selection_context ( self ):
1554
+ with self . _group_selection_context ( ):
1556
1555
data = self ._selected_obj
1557
1556
df = data if data .ndim == 2 else data .to_frame ()
1558
1557
result = self ._transform_with_numba (
@@ -1954,7 +1953,7 @@ def var(self, ddof: int = 1):
1954
1953
)
1955
1954
else :
1956
1955
func = lambda x : x .var (ddof = ddof )
1957
- with group_selection_context ( self ):
1956
+ with self . _group_selection_context ( ):
1958
1957
return self ._python_agg_general (func )
1959
1958
1960
1959
@final
@@ -2146,7 +2145,7 @@ def ohlc(self) -> DataFrame:
2146
2145
2147
2146
@doc (DataFrame .describe )
2148
2147
def describe (self , ** kwargs ):
2149
- with group_selection_context ( self ):
2148
+ with self . _group_selection_context ( ):
2150
2149
result = self .apply (lambda x : x .describe (** kwargs ))
2151
2150
if self .axis == 1 :
2152
2151
return result .T
@@ -2530,7 +2529,7 @@ def nth(
2530
2529
nth_values = list (set (n ))
2531
2530
2532
2531
nth_array = np .array (nth_values , dtype = np .intp )
2533
- with group_selection_context ( self ):
2532
+ with self . _group_selection_context ( ):
2534
2533
2535
2534
mask_left = np .in1d (self ._cumcount_array (), nth_array )
2536
2535
mask_right = np .in1d (
@@ -2827,7 +2826,7 @@ def ngroup(self, ascending: bool = True):
2827
2826
5 0
2828
2827
dtype: int64
2829
2828
"""
2830
- with group_selection_context ( self ):
2829
+ with self . _group_selection_context ( ):
2831
2830
index = self ._selected_obj .index
2832
2831
result = self ._obj_1d_constructor (
2833
2832
self .grouper .group_info [0 ], index , dtype = np .int64
@@ -2891,7 +2890,7 @@ def cumcount(self, ascending: bool = True):
2891
2890
5 0
2892
2891
dtype: int64
2893
2892
"""
2894
- with group_selection_context ( self ):
2893
+ with self . _group_selection_context ( ):
2895
2894
index = self ._selected_obj ._get_axis (self .axis )
2896
2895
cumcounts = self ._cumcount_array (ascending = ascending )
2897
2896
return self ._obj_1d_constructor (cumcounts , index )
0 commit comments