Skip to content

Commit d8e1ba5

Browse files
authored
REF: share _wrap_aggregated_output (#43446)
1 parent 8578696 commit d8e1ba5

File tree

2 files changed

+61
-61
lines changed

2 files changed

+61
-61
lines changed

pandas/core/groupby/generic.py

+15-60
Original file line numberDiff line numberDiff line change
@@ -354,35 +354,17 @@ def array_func(values: ArrayLike) -> ArrayLike:
354354
)
355355
return self._reindex_output(ser)
356356

357-
def _wrap_aggregated_output(
358-
self,
359-
output: Mapping[base.OutputKey, Series | ArrayLike],
357+
def _indexed_output_to_ndframe(
358+
self, output: Mapping[base.OutputKey, ArrayLike]
360359
) -> Series:
361360
"""
362-
Wraps the output of a SeriesGroupBy aggregation into the expected result.
363-
364-
Parameters
365-
----------
366-
output : Mapping[base.OutputKey, Union[Series, ArrayLike]]
367-
Data to wrap.
368-
369-
Returns
370-
-------
371-
Series
372-
373-
Notes
374-
-----
375-
In the vast majority of cases output will only contain one element.
376-
The exception is operations that expand dimensions, like ohlc.
361+
Wrap the dict result of a GroupBy aggregation into a Series.
377362
"""
378363
assert len(output) == 1
379-
380-
name = self.obj.name
381-
index = self.grouper.result_index
382364
values = next(iter(output.values()))
383-
384-
result = self.obj._constructor(values, index=index, name=name)
385-
return self._reindex_output(result)
365+
result = self.obj._constructor(values)
366+
result.name = self.obj.name
367+
return result
386368

387369
def _wrap_transformed_output(
388370
self, output: Mapping[base.OutputKey, Series | ArrayLike]
@@ -1614,46 +1596,19 @@ def _insert_inaxis_grouper_inplace(self, result: DataFrame) -> None:
16141596
if in_axis and name not in columns:
16151597
result.insert(0, name, lev)
16161598

1617-
def _wrap_aggregated_output(
1618-
self,
1619-
output: Mapping[base.OutputKey, Series | ArrayLike],
1599+
def _indexed_output_to_ndframe(
1600+
self, output: Mapping[base.OutputKey, ArrayLike]
16201601
) -> DataFrame:
16211602
"""
1622-
Wraps the output of DataFrameGroupBy aggregations into the expected result.
1623-
1624-
Parameters
1625-
----------
1626-
output : Mapping[base.OutputKey, Union[Series, np.ndarray]]
1627-
Data to wrap.
1628-
1629-
Returns
1630-
-------
1631-
DataFrame
1603+
Wrap the dict result of a GroupBy aggregation into a DataFrame.
16321604
"""
1633-
if isinstance(output, DataFrame):
1634-
result = output
1635-
else:
1636-
indexed_output = {key.position: val for key, val in output.items()}
1637-
columns = Index([key.label for key in output])
1638-
columns._set_names(self._obj_with_exclusions._get_axis(1 - self.axis).names)
1639-
1640-
result = self.obj._constructor(indexed_output)
1641-
result.columns = columns
1642-
1643-
if not self.as_index:
1644-
self._insert_inaxis_grouper_inplace(result)
1645-
result = result._consolidate()
1646-
else:
1647-
result.index = self.grouper.result_index
1648-
1649-
if self.axis == 1:
1650-
result = result.T
1651-
if result.index.equals(self.obj.index):
1652-
# Retain e.g. DatetimeIndex/TimedeltaIndex freq
1653-
result.index = self.obj.index.copy()
1654-
# TODO: Do this more systematically
1605+
indexed_output = {key.position: val for key, val in output.items()}
1606+
columns = Index([key.label for key in output])
1607+
columns._set_names(self._obj_with_exclusions._get_axis(1 - self.axis).names)
16551608

1656-
return self._reindex_output(result)
1609+
result = self.obj._constructor(indexed_output)
1610+
result.columns = columns
1611+
return result
16571612

16581613
def _wrap_transformed_output(
16591614
self, output: Mapping[base.OutputKey, Series | ArrayLike]

pandas/core/groupby/groupby.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -1095,9 +1095,54 @@ def _set_result_index_ordered(
10951095

10961096
return result
10971097

1098-
def _wrap_aggregated_output(self, output: Mapping[base.OutputKey, ArrayLike]):
1098+
def _indexed_output_to_ndframe(
1099+
self, result: Mapping[base.OutputKey, ArrayLike]
1100+
) -> Series | DataFrame:
10991101
raise AbstractMethodError(self)
11001102

1103+
def _wrap_aggregated_output(
1104+
self, output: Series | DataFrame | Mapping[base.OutputKey, ArrayLike]
1105+
):
1106+
"""
1107+
Wraps the output of GroupBy aggregations into the expected result.
1108+
1109+
Parameters
1110+
----------
1111+
output : Series, DataFrame, or Mapping[base.OutputKey, ArrayLike]
1112+
Data to wrap.
1113+
1114+
Returns
1115+
-------
1116+
Series or DataFrame
1117+
"""
1118+
1119+
if isinstance(output, (Series, DataFrame)):
1120+
# We get here (for DataFrameGroupBy) if we used Manager.grouped_reduce,
1121+
# in which case our columns are already set correctly.
1122+
# ATM we do not get here for SeriesGroupBy; when we do, we will
1123+
# need to require that result.name already match self.obj.name
1124+
result = output
1125+
else:
1126+
result = self._indexed_output_to_ndframe(output)
1127+
1128+
if not self.as_index:
1129+
# `not self.as_index` is only relevant for DataFrameGroupBy,
1130+
# enforced in __init__
1131+
self._insert_inaxis_grouper_inplace(result)
1132+
result = result._consolidate()
1133+
else:
1134+
result.index = self.grouper.result_index
1135+
1136+
if self.axis == 1:
1137+
# Only relevant for DataFrameGroupBy, no-op for SeriesGroupBy
1138+
result = result.T
1139+
if result.index.equals(self.obj.index):
1140+
# Retain e.g. DatetimeIndex/TimedeltaIndex freq
1141+
result.index = self.obj.index.copy()
1142+
# TODO: Do this more systematically
1143+
1144+
return self._reindex_output(result)
1145+
11011146
def _wrap_transformed_output(self, output: Mapping[base.OutputKey, ArrayLike]):
11021147
raise AbstractMethodError(self)
11031148

0 commit comments

Comments
 (0)