@@ -1177,6 +1177,18 @@ def _resolve_numeric_only(self, numeric_only: bool | lib.NoDefault) -> bool:
1177
1177
# expected "bool")
1178
1178
return numeric_only # type: ignore[return-value]
1179
1179
1180
+ @cache_readonly
1181
+ def _group_keys_index (self ) -> Index :
1182
+ # The index to use for the result of Groupby Aggregations.
1183
+ # This _may_ be redundant with self.grouper.result_index, but that
1184
+ # has not been conclusively proven yet.
1185
+ keys = self .grouper ._get_group_keys ()
1186
+ if self .grouper .nkeys > 1 :
1187
+ index = MultiIndex .from_tuples (keys , names = self .grouper .names )
1188
+ else :
1189
+ index = Index ._with_infer (keys , name = self .grouper .names [0 ])
1190
+ return index
1191
+
1180
1192
# -----------------------------------------------------------------
1181
1193
# numba
1182
1194
@@ -1244,15 +1256,15 @@ def _aggregate_with_numba(self, data, func, *args, engine_kwargs=None, **kwargs)
1244
1256
data and indices into a Numba jitted function.
1245
1257
"""
1246
1258
starts , ends , sorted_index , sorted_data = self ._numba_prep (func , data )
1247
- group_keys = self .grouper . _get_group_keys ()
1259
+ index = self ._group_keys_index
1248
1260
1249
1261
numba_agg_func = numba_ .generate_numba_agg_func (kwargs , func , engine_kwargs )
1250
1262
result = numba_agg_func (
1251
1263
sorted_data ,
1252
1264
sorted_index ,
1253
1265
starts ,
1254
1266
ends ,
1255
- len (group_keys ),
1267
+ len (index ),
1256
1268
len (data .columns ),
1257
1269
* args ,
1258
1270
)
@@ -1261,10 +1273,6 @@ def _aggregate_with_numba(self, data, func, *args, engine_kwargs=None, **kwargs)
1261
1273
if cache_key not in NUMBA_FUNC_CACHE :
1262
1274
NUMBA_FUNC_CACHE [cache_key ] = numba_agg_func
1263
1275
1264
- if self .grouper .nkeys > 1 :
1265
- index = MultiIndex .from_tuples (group_keys , names = self .grouper .names )
1266
- else :
1267
- index = Index (group_keys , name = self .grouper .names [0 ])
1268
1276
return result , index
1269
1277
1270
1278
# -----------------------------------------------------------------
0 commit comments