3232# SPDX-License-Identifier: BSD-3-Clause
3333# ruff: noqa: ERA001
3434import logging
35+ from collections .abc import Callable
3536from types import MethodType
36- from typing import Any , Callable
37+ from typing import Any
3738
3839from pandas .core .groupby .ops import _is_indexed_like
3940
@@ -51,18 +52,18 @@ def run_groupwise_apply(
5152 args : tuple [Any , ...] = (),
5253 ** kwargs : Any ,
5354):
54- """Patch GroupBy.grouper.apply , applying func to each group in parallel."""
55+ """Patch GroupBy._grouper.apply_groupwise , applying func to each group in parallel."""
5556
56- def apply (self , f , data , axis = 0 ):
57- # patching https://github.com/pandas-dev/pandas/blob/v2.1.4 /pandas/core/groupby/ops.py#L890
57+ def apply (self , f , data ):
58+ # patching https://github.com/pandas-dev/pandas/blob/v3.0.1 /pandas/core/groupby/ops.py#L1014
5859 # with a multiprocessing_imap
5960 mutated = False
60- splitter = self ._get_splitter (data , axis = axis )
61- group_keys = self .group_keys_seq
61+ splitter = self ._get_splitter (data )
62+ group_keys = self .result_index
6263 result_values = []
6364
6465 # This calls DataSplitter.__iter__
65- zipped = zip (group_keys , splitter )
66+ zipped = zip (group_keys , splitter , strict = True )
6667
6768 # rewrite the original for-loop into an imap
6869 def _run_apply (args ):
@@ -91,12 +92,13 @@ def _run_apply(args):
9192 # original for-loop leftover
9293 for res , group_axes in zipped :
9394 # no changes made below this line
94- if not mutated and not _is_indexed_like (res , group_axes , axis ):
95+ if not mutated and not _is_indexed_like (res , group_axes ):
9596 mutated = True
9697 result_values .append (res )
9798 # getattr pattern for __name__ is needed for functools.partial objects
9899 if len (group_keys ) == 0 and getattr (f , "__name__" , None ) in [
99100 "skew" ,
101+ "kurt" ,
100102 "sum" ,
101103 "prod" ,
102104 ]:
@@ -109,9 +111,10 @@ def _run_apply(args):
109111
110112 # overwrite apply method and restore after execution
111113 attr = "apply_groupwise"
112- original_apply = getattr (df_or_series .grouper , attr )
113- setattr (df_or_series .grouper , attr , MethodType (apply , df_or_series .grouper ))
114+ grouper = df_or_series ._grouper # noqa: SLF001
115+ original_apply = getattr (grouper , attr )
116+ setattr (grouper , attr , MethodType (apply , grouper ))
114117 try :
115118 return df_or_series .apply (func , * args , ** kwargs )
116119 finally :
117- setattr (df_or_series . grouper , attr , original_apply )
120+ setattr (grouper , attr , original_apply )
0 commit comments