Skip to content

REF: De-duplicate agg_series #41210

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
Apr 29, 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
2 changes: 1 addition & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,7 +1686,7 @@ def _insert_inaxis_grouper_inplace(self, result: DataFrame) -> None:

def _wrap_aggregated_output(
self,
output: Mapping[base.OutputKey, Series | np.ndarray],
output: Mapping[base.OutputKey, Series | ArrayLike],
) -> DataFrame:
"""
Wraps the output of DataFrameGroupBy aggregations into the expected result.
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ def _set_result_index_ordered(

return result

def _wrap_aggregated_output(self, output: Mapping[base.OutputKey, np.ndarray]):
def _wrap_aggregated_output(self, output: Mapping[base.OutputKey, ArrayLike]):
raise AbstractMethodError(self)

def _wrap_transformed_output(self, output: Mapping[base.OutputKey, ArrayLike]):
Expand Down Expand Up @@ -1222,7 +1222,7 @@ def _python_agg_general(self, func, *args, **kwargs):
f = lambda x: func(x, *args, **kwargs)

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

for idx, obj in enumerate(self._iterate_slices()):
name = obj.name
Expand Down
28 changes: 13 additions & 15 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,8 @@ def _cython_operation(
**kwargs,
)

def agg_series(self, obj: Series, func: F):
@final
def agg_series(self, obj: Series, func: F) -> tuple[ArrayLike, np.ndarray]:
# Caller is responsible for checking ngroups != 0
assert self.ngroups != 0

Expand Down Expand Up @@ -952,8 +953,9 @@ def agg_series(self, obj: Series, func: F):
raise
return self._aggregate_series_pure_python(obj, func)

@final
def _aggregate_series_fast(self, obj: Series, func: F):
def _aggregate_series_fast(
self, obj: Series, func: F
) -> tuple[ArrayLike, np.ndarray]:
# At this point we have already checked that
# - obj.index is not a MultiIndex
# - obj is backed by an ndarray, not ExtensionArray
Expand Down Expand Up @@ -1157,18 +1159,14 @@ def groupings(self) -> list[grouper.Grouping]:
for lvl, name in zip(self.levels, self.names)
]

def agg_series(self, obj: Series, func: F):
# Caller is responsible for checking ngroups != 0
assert self.ngroups != 0
assert len(self.bins) > 0 # otherwise we'd get IndexError in get_result

if is_extension_array_dtype(obj.dtype):
# preempt SeriesBinGrouper from raising TypeError
return self._aggregate_series_pure_python(obj, func)

elif obj.index._has_complex_internals:
return self._aggregate_series_pure_python(obj, func)

def _aggregate_series_fast(
self, obj: Series, func: F
) -> tuple[ArrayLike, np.ndarray]:
# At this point we have already checked that
# - obj.index is not a MultiIndex
# - obj is backed by an ndarray, not ExtensionArray
# - ngroups != 0
# - len(self.bins) > 0
grouper = libreduction.SeriesBinGrouper(obj, func, self.bins)
return grouper.get_result()

Expand Down