Skip to content

REF: remove iterate_slices #51622

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 1 commit into from
Feb 25, 2023
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
48 changes: 11 additions & 37 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Any,
Callable,
Hashable,
Iterable,
Literal,
Mapping,
NamedTuple,
Expand Down Expand Up @@ -1337,38 +1336,27 @@ def _python_agg_general(self, func, *args, **kwargs):
func = com.is_builtin_func(func)
f = lambda x: func(x, *args, **kwargs)

# iterate through "columns" ex exclusions to populate output dict
output: dict[base.OutputKey, ArrayLike] = {}

if self.ngroups == 0:
# e.g. test_evaluate_with_empty_groups different path gets different
# result dtype in empty case.
return self._python_apply_general(f, self._selected_obj, is_agg=True)

for idx, obj in enumerate(self._iterate_slices()):
name = obj.name
result = self.grouper.agg_series(obj, f)
key = base.OutputKey(label=name, position=idx)
output[key] = result
obj = self._obj_with_exclusions
if self.axis == 1:
obj = obj.T

if not output:
if not len(obj.columns):
# e.g. test_margins_no_values_no_cols
return self._python_apply_general(f, self._selected_obj)

res = self._indexed_output_to_ndframe(output)
return self._wrap_aggregated_output(res)

def _iterate_slices(self) -> Iterable[Series]:
obj = self._obj_with_exclusions
if self.axis == 1:
obj = obj.T
output: dict[int, ArrayLike] = {}
for idx, (name, ser) in enumerate(obj.items()):
result = self.grouper.agg_series(ser, f)
output[idx] = result

if isinstance(obj, Series):
# Occurs when doing DataFrameGroupBy(...)["X"]
yield obj
else:
for label, values in obj.items():
yield values
res = self.obj._constructor(output)
res.columns = obj.columns.copy(deep=False)
return self._wrap_aggregated_output(res)

def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
if self.grouper.nkeys != 1:
Expand Down Expand Up @@ -1830,20 +1818,6 @@ def _get_data_to_aggregate(
mgr = mgr.get_numeric_data(copy=False)
return mgr

def _indexed_output_to_ndframe(
self, output: Mapping[base.OutputKey, ArrayLike]
) -> DataFrame:
"""
Wrap the dict result of a GroupBy aggregation into a DataFrame.
"""
indexed_output = {key.position: val for key, val in output.items()}
columns = Index([key.label for key in output])
columns._set_names(self._obj_with_exclusions._get_axis(1 - self.axis).names)

result = self.obj._constructor(indexed_output)
result.columns = columns
return result

def _wrap_agged_manager(self, mgr: Manager2D) -> DataFrame:
return self.obj._constructor(mgr)

Expand Down
5 changes: 0 additions & 5 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,11 +1099,6 @@ def _insert_inaxis_grouper(self, result: Series | DataFrame) -> DataFrame:

return result

def _indexed_output_to_ndframe(
self, result: Mapping[base.OutputKey, ArrayLike]
) -> Series | DataFrame:
raise AbstractMethodError(self)

@final
def _maybe_transpose_result(self, result: NDFrameT) -> NDFrameT:
if self.axis == 1:
Expand Down