Skip to content

Commit d4b2103

Browse files
authored
CLN: remove index keyword from _wrap_aggregated_output (#41128)
1 parent b6cb94c commit d4b2103

File tree

2 files changed

+26
-80
lines changed

2 files changed

+26
-80
lines changed

pandas/core/groupby/generic.py

+23-75
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,12 @@ def _aggregate_multiple_funcs(self, arg):
336336
# let higher level handle
337337
return results
338338

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
347345

348346
def _cython_agg_general(
349347
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
@@ -371,78 +369,36 @@ def _cython_agg_general(
371369
if not output:
372370
raise DataError("No numeric types to aggregate")
373371

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)
408373

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
423374
def _wrap_aggregated_output(
424375
self,
425-
output: Mapping[base.OutputKey, Series | np.ndarray],
426-
index: Index | None,
427-
) -> FrameOrSeriesUnion:
376+
output: Mapping[base.OutputKey, Series | ArrayLike],
377+
) -> Series:
428378
"""
429379
Wraps the output of a SeriesGroupBy aggregation into the expected result.
430380
431381
Parameters
432382
----------
433-
output : Mapping[base.OutputKey, Union[Series, np.ndarray]]
383+
output : Mapping[base.OutputKey, Union[Series, ArrayLike]]
434384
Data to wrap.
435385
436386
Returns
437387
-------
438-
Series or DataFrame
388+
Series
439389
440390
Notes
441391
-----
442392
In the vast majority of cases output will only contain one element.
443393
The exception is operations that expand dimensions, like ohlc.
444394
"""
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)
446402
return self._reindex_output(result)
447403

448404
def _wrap_transformed_output(
@@ -466,7 +422,10 @@ def _wrap_transformed_output(
466422
for consistency with DataFrame methods and _wrap_aggregated_output.
467423
"""
468424
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)
470429

471430
# No transformations increase the ndim of the result
472431
assert isinstance(result, Series)
@@ -1115,14 +1074,6 @@ def _iterate_slices(self) -> Iterable[Series]:
11151074
def _cython_agg_general(
11161075
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
11171076
) -> 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:
11261077
# Note: we never get here with how="ohlc"; that goes through SeriesGroupBy
11271078

11281079
data: Manager2D = self._get_data_to_aggregate()
@@ -1186,11 +1137,9 @@ def py_fallback(values: ArrayLike) -> ArrayLike:
11861137
sgb = get_groupby(obj, self.grouper, observed=True)
11871138
result = sgb.aggregate(lambda x: alt(x, axis=self.axis))
11881139

1189-
assert isinstance(result, (Series, DataFrame)) # for mypy
11901140
# In the case of object dtype block, it may have been split
11911141
# in the operation. We un-split here.
11921142
result = result._consolidate()
1193-
assert isinstance(result, (Series, DataFrame)) # for mypy
11941143
# unwrap DataFrame/Series to get array
11951144
mgr = result._mgr
11961145
arrays = mgr.arrays
@@ -1226,7 +1175,7 @@ def array_func(values: ArrayLike) -> ArrayLike:
12261175
if not len(new_mgr):
12271176
raise DataError("No numeric types to aggregate")
12281177

1229-
return new_mgr
1178+
return self._wrap_agged_manager(new_mgr)
12301179

12311180
def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
12321181
if self.grouper.nkeys != 1:
@@ -1733,7 +1682,6 @@ def _insert_inaxis_grouper_inplace(self, result: DataFrame) -> None:
17331682
def _wrap_aggregated_output(
17341683
self,
17351684
output: Mapping[base.OutputKey, Series | np.ndarray],
1736-
index: Index | None,
17371685
) -> DataFrame:
17381686
"""
17391687
Wraps the output of DataFrameGroupBy aggregations into the expected result.

pandas/core/groupby/groupby.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,7 @@ def _set_result_index_ordered(
10561056

10571057
return result
10581058

1059-
def _wrap_aggregated_output(
1060-
self, output: Mapping[base.OutputKey, np.ndarray], index: Index | None
1061-
):
1059+
def _wrap_aggregated_output(self, output: Mapping[base.OutputKey, np.ndarray]):
10621060
raise AbstractMethodError(self)
10631061

10641062
def _wrap_transformed_output(self, output: Mapping[base.OutputKey, ArrayLike]):
@@ -1259,7 +1257,7 @@ def _python_agg_general(self, func, *args, **kwargs):
12591257
if not output:
12601258
return self._python_apply_general(f, self._selected_obj)
12611259

1262-
return self._wrap_aggregated_output(output, index=self.grouper.result_index)
1260+
return self._wrap_aggregated_output(output)
12631261

12641262
@final
12651263
def _agg_general(
@@ -2786,7 +2784,7 @@ def _get_cythonized_result(
27862784
raise TypeError(error_msg)
27872785

27882786
if aggregate:
2789-
return self._wrap_aggregated_output(output, index=self.grouper.result_index)
2787+
return self._wrap_aggregated_output(output)
27902788
else:
27912789
return self._wrap_transformed_output(output)
27922790

0 commit comments

Comments
 (0)