Skip to content

REF: share _wrap_transformed_output #43449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
29 changes: 27 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
):
Expand Down Expand Up @@ -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)
Expand Down