@@ -336,14 +336,12 @@ def _aggregate_multiple_funcs(self, arg):
336
336
# let higher level handle
337
337
return results
338
338
339
- # Argument 1 to "_wrap_aggregated_output" of "SeriesGroupBy" has
340
- # incompatible type "Dict[OutputKey, Union[DataFrame,
341
- # Series]]";
342
- # expected "Mapping[OutputKey, Union[Series, ndarray]]"
343
- output = self ._wrap_aggregated_output (
344
- results , index = None # type: ignore[arg-type]
345
- )
346
- return self .obj ._constructor_expanddim (output , columns = columns )
339
+ indexed_output = {key .position : val for key , val in results .items ()}
340
+ output = self .obj ._constructor_expanddim (indexed_output , index = None )
341
+ output .columns = Index (key .label for key in results )
342
+
343
+ output = self ._reindex_output (output )
344
+ return output
347
345
348
346
def _cython_agg_general (
349
347
self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
@@ -371,78 +369,36 @@ def _cython_agg_general(
371
369
if not output :
372
370
raise DataError ("No numeric types to aggregate" )
373
371
374
- # error: Argument 1 to "_wrap_aggregated_output" of "BaseGroupBy" has
375
- # incompatible type "Dict[OutputKey, Union[ndarray, DatetimeArray]]";
376
- # expected "Mapping[OutputKey, ndarray]"
377
- return self ._wrap_aggregated_output (
378
- output , index = self .grouper .result_index # type: ignore[arg-type]
379
- )
380
-
381
- # TODO: index should not be Optional - see GH 35490
382
- def _wrap_series_output (
383
- self ,
384
- output : Mapping [base .OutputKey , Series | ArrayLike ],
385
- index : Index | None ,
386
- ) -> FrameOrSeriesUnion :
387
- """
388
- Wraps the output of a SeriesGroupBy operation into the expected result.
389
-
390
- Parameters
391
- ----------
392
- output : Mapping[base.OutputKey, Union[Series, np.ndarray, ExtensionArray]]
393
- Data to wrap.
394
- index : pd.Index or None
395
- Index to apply to the output.
396
-
397
- Returns
398
- -------
399
- Series or DataFrame
400
-
401
- Notes
402
- -----
403
- In the vast majority of cases output and columns will only contain one
404
- element. The exception is operations that expand dimensions, like ohlc.
405
- """
406
- indexed_output = {key .position : val for key , val in output .items ()}
407
- columns = Index (key .label for key in output )
372
+ return self ._wrap_aggregated_output (output )
408
373
409
- result : FrameOrSeriesUnion
410
- if len (output ) > 1 :
411
- result = self .obj ._constructor_expanddim (indexed_output , index = index )
412
- result .columns = columns
413
- elif not columns .empty :
414
- result = self .obj ._constructor (
415
- indexed_output [0 ], index = index , name = columns [0 ]
416
- )
417
- else :
418
- result = self .obj ._constructor_expanddim ()
419
-
420
- return result
421
-
422
- # TODO: Remove index argument, use self.grouper.result_index, see GH 35490
423
374
def _wrap_aggregated_output (
424
375
self ,
425
- output : Mapping [base .OutputKey , Series | np .ndarray ],
426
- index : Index | None ,
427
- ) -> FrameOrSeriesUnion :
376
+ output : Mapping [base .OutputKey , Series | ArrayLike ],
377
+ ) -> Series :
428
378
"""
429
379
Wraps the output of a SeriesGroupBy aggregation into the expected result.
430
380
431
381
Parameters
432
382
----------
433
- output : Mapping[base.OutputKey, Union[Series, np.ndarray ]]
383
+ output : Mapping[base.OutputKey, Union[Series, ArrayLike ]]
434
384
Data to wrap.
435
385
436
386
Returns
437
387
-------
438
- Series or DataFrame
388
+ Series
439
389
440
390
Notes
441
391
-----
442
392
In the vast majority of cases output will only contain one element.
443
393
The exception is operations that expand dimensions, like ohlc.
444
394
"""
445
- result = self ._wrap_series_output (output = output , index = index )
395
+ assert len (output ) == 1
396
+
397
+ name = self .obj .name
398
+ index = self .grouper .result_index
399
+ values = next (iter (output .values ()))
400
+
401
+ result = self .obj ._constructor (values , index = index , name = name )
446
402
return self ._reindex_output (result )
447
403
448
404
def _wrap_transformed_output (
@@ -466,7 +422,10 @@ def _wrap_transformed_output(
466
422
for consistency with DataFrame methods and _wrap_aggregated_output.
467
423
"""
468
424
assert len (output ) == 1
469
- result = self ._wrap_series_output (output = output , index = self .obj .index )
425
+
426
+ name = self .obj .name
427
+ values = next (iter (output .values ()))
428
+ result = self .obj ._constructor (values , index = self .obj .index , name = name )
470
429
471
430
# No transformations increase the ndim of the result
472
431
assert isinstance (result , Series )
@@ -1115,14 +1074,6 @@ def _iterate_slices(self) -> Iterable[Series]:
1115
1074
def _cython_agg_general (
1116
1075
self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
1117
1076
) -> DataFrame :
1118
- agg_mgr = self ._cython_agg_manager (
1119
- how , alt = alt , numeric_only = numeric_only , min_count = min_count
1120
- )
1121
- return self ._wrap_agged_manager (agg_mgr )
1122
-
1123
- def _cython_agg_manager (
1124
- self , how : str , alt = None , numeric_only : bool = True , min_count : int = - 1
1125
- ) -> Manager2D :
1126
1077
# Note: we never get here with how="ohlc"; that goes through SeriesGroupBy
1127
1078
1128
1079
data : Manager2D = self ._get_data_to_aggregate ()
@@ -1186,11 +1137,9 @@ def py_fallback(values: ArrayLike) -> ArrayLike:
1186
1137
sgb = get_groupby (obj , self .grouper , observed = True )
1187
1138
result = sgb .aggregate (lambda x : alt (x , axis = self .axis ))
1188
1139
1189
- assert isinstance (result , (Series , DataFrame )) # for mypy
1190
1140
# In the case of object dtype block, it may have been split
1191
1141
# in the operation. We un-split here.
1192
1142
result = result ._consolidate ()
1193
- assert isinstance (result , (Series , DataFrame )) # for mypy
1194
1143
# unwrap DataFrame/Series to get array
1195
1144
mgr = result ._mgr
1196
1145
arrays = mgr .arrays
@@ -1226,7 +1175,7 @@ def array_func(values: ArrayLike) -> ArrayLike:
1226
1175
if not len (new_mgr ):
1227
1176
raise DataError ("No numeric types to aggregate" )
1228
1177
1229
- return new_mgr
1178
+ return self . _wrap_agged_manager ( new_mgr )
1230
1179
1231
1180
def _aggregate_frame (self , func , * args , ** kwargs ) -> DataFrame :
1232
1181
if self .grouper .nkeys != 1 :
@@ -1733,7 +1682,6 @@ def _insert_inaxis_grouper_inplace(self, result: DataFrame) -> None:
1733
1682
def _wrap_aggregated_output (
1734
1683
self ,
1735
1684
output : Mapping [base .OutputKey , Series | np .ndarray ],
1736
- index : Index | None ,
1737
1685
) -> DataFrame :
1738
1686
"""
1739
1687
Wraps the output of DataFrameGroupBy aggregations into the expected result.
0 commit comments