diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 8bb8f00b4c406..9f45a6665ca5c 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -366,36 +366,6 @@ def _indexed_output_to_ndframe( result.name = self.obj.name return result - def _wrap_transformed_output( - self, output: Mapping[base.OutputKey, Series | ArrayLike] - ) -> Series: - """ - Wraps the output of a SeriesGroupBy aggregation into the expected result. - - Parameters - ---------- - output : dict[base.OutputKey, Union[Series, np.ndarray, ExtensionArray]] - Dict with a sole key of 0 and a value of the result values. - - Returns - ------- - Series - - Notes - ----- - output should always contain one element. It is specified as a dict - for consistency with DataFrame methods and _wrap_aggregated_output. - """ - assert len(output) == 1 - - name = self.obj.name - values = next(iter(output.values())) - result = self.obj._constructor(values, index=self.obj.index, name=name) - - # No transformations increase the ndim of the result - assert isinstance(result, Series) - return result - def _wrap_applied_output( self, data: Series, @@ -1610,36 +1580,6 @@ def _indexed_output_to_ndframe( result.columns = columns return result - def _wrap_transformed_output( - self, output: Mapping[base.OutputKey, Series | ArrayLike] - ) -> DataFrame: - """ - Wraps the output of DataFrameGroupBy transformations into the expected result. - - Parameters - ---------- - output : Mapping[base.OutputKey, Union[Series, np.ndarray, ExtensionArray]] - Data to wrap. - - Returns - ------- - DataFrame - """ - indexed_output = {key.position: val for key, val in output.items()} - result = self.obj._constructor(indexed_output) - - if self.axis == 1: - result = result.T - result.columns = self.obj.columns - else: - columns = Index(key.label for key in output) - columns._set_names(self.obj._get_axis(1 - self.axis).names) - result.columns = columns - - result.index = self.obj.index - - return result - def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame: if not self.as_index: # GH 41998 - empty mgr always gets index of length 0 diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index a60ec29581337..0547b9209ae65 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1100,6 +1100,7 @@ def _indexed_output_to_ndframe( ) -> Series | DataFrame: raise AbstractMethodError(self) + @final def _wrap_aggregated_output( self, output: Series | DataFrame | Mapping[base.OutputKey, ArrayLike] ): @@ -1143,8 +1144,32 @@ def _wrap_aggregated_output( return self._reindex_output(result) - def _wrap_transformed_output(self, output: Mapping[base.OutputKey, ArrayLike]): - raise AbstractMethodError(self) + @final + def _wrap_transformed_output( + self, output: Mapping[base.OutputKey, ArrayLike] + ) -> Series | DataFrame: + """ + Wraps the output of GroupBy transformations into the expected result. + + Parameters + ---------- + output : Mapping[base.OutputKey, ArrayLike] + Data to wrap. + + Returns + ------- + Series or DataFrame + Series for SeriesGroupBy, DataFrame for DataFrameGroupBy + """ + result = self._indexed_output_to_ndframe(output) + + if self.axis == 1: + # Only relevant for DataFrameGroupBy + result = result.T + result.columns = self.obj.columns + + result.index = self.obj.index + return result def _wrap_applied_output(self, data, keys, values, not_indexed_same: bool = False): raise AbstractMethodError(self)