Skip to content

Commit cce7d10

Browse files
authored
REF: share _wrap_transformed_output (#43449)
1 parent 4b12ae3 commit cce7d10

File tree

2 files changed

+27
-62
lines changed

2 files changed

+27
-62
lines changed

pandas/core/groupby/generic.py

-60
Original file line numberDiff line numberDiff line change
@@ -366,36 +366,6 @@ def _indexed_output_to_ndframe(
366366
result.name = self.obj.name
367367
return result
368368

369-
def _wrap_transformed_output(
370-
self, output: Mapping[base.OutputKey, Series | ArrayLike]
371-
) -> Series:
372-
"""
373-
Wraps the output of a SeriesGroupBy aggregation into the expected result.
374-
375-
Parameters
376-
----------
377-
output : dict[base.OutputKey, Union[Series, np.ndarray, ExtensionArray]]
378-
Dict with a sole key of 0 and a value of the result values.
379-
380-
Returns
381-
-------
382-
Series
383-
384-
Notes
385-
-----
386-
output should always contain one element. It is specified as a dict
387-
for consistency with DataFrame methods and _wrap_aggregated_output.
388-
"""
389-
assert len(output) == 1
390-
391-
name = self.obj.name
392-
values = next(iter(output.values()))
393-
result = self.obj._constructor(values, index=self.obj.index, name=name)
394-
395-
# No transformations increase the ndim of the result
396-
assert isinstance(result, Series)
397-
return result
398-
399369
def _wrap_applied_output(
400370
self,
401371
data: Series,
@@ -1603,36 +1573,6 @@ def _indexed_output_to_ndframe(
16031573
result.columns = columns
16041574
return result
16051575

1606-
def _wrap_transformed_output(
1607-
self, output: Mapping[base.OutputKey, Series | ArrayLike]
1608-
) -> DataFrame:
1609-
"""
1610-
Wraps the output of DataFrameGroupBy transformations into the expected result.
1611-
1612-
Parameters
1613-
----------
1614-
output : Mapping[base.OutputKey, Union[Series, np.ndarray, ExtensionArray]]
1615-
Data to wrap.
1616-
1617-
Returns
1618-
-------
1619-
DataFrame
1620-
"""
1621-
indexed_output = {key.position: val for key, val in output.items()}
1622-
result = self.obj._constructor(indexed_output)
1623-
1624-
if self.axis == 1:
1625-
result = result.T
1626-
result.columns = self.obj.columns
1627-
else:
1628-
columns = Index(key.label for key in output)
1629-
columns._set_names(self.obj._get_axis(1 - self.axis).names)
1630-
result.columns = columns
1631-
1632-
result.index = self.obj.index
1633-
1634-
return result
1635-
16361576
def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
16371577
if not self.as_index:
16381578
# GH 41998 - empty mgr always gets index of length 0

pandas/core/groupby/groupby.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,7 @@ def _indexed_output_to_ndframe(
11001100
) -> Series | DataFrame:
11011101
raise AbstractMethodError(self)
11021102

1103+
@final
11031104
def _wrap_aggregated_output(
11041105
self, output: Series | DataFrame | Mapping[base.OutputKey, ArrayLike]
11051106
):
@@ -1143,8 +1144,32 @@ def _wrap_aggregated_output(
11431144

11441145
return self._reindex_output(result)
11451146

1146-
def _wrap_transformed_output(self, output: Mapping[base.OutputKey, ArrayLike]):
1147-
raise AbstractMethodError(self)
1147+
@final
1148+
def _wrap_transformed_output(
1149+
self, output: Mapping[base.OutputKey, ArrayLike]
1150+
) -> Series | DataFrame:
1151+
"""
1152+
Wraps the output of GroupBy transformations into the expected result.
1153+
1154+
Parameters
1155+
----------
1156+
output : Mapping[base.OutputKey, ArrayLike]
1157+
Data to wrap.
1158+
1159+
Returns
1160+
-------
1161+
Series or DataFrame
1162+
Series for SeriesGroupBy, DataFrame for DataFrameGroupBy
1163+
"""
1164+
result = self._indexed_output_to_ndframe(output)
1165+
1166+
if self.axis == 1:
1167+
# Only relevant for DataFrameGroupBy
1168+
result = result.T
1169+
result.columns = self.obj.columns
1170+
1171+
result.index = self.obj.index
1172+
return result
11481173

11491174
def _wrap_applied_output(self, data, keys, values, not_indexed_same: bool = False):
11501175
raise AbstractMethodError(self)

0 commit comments

Comments
 (0)