Skip to content

REF: BaseGroupBy methods belong in GroupBy #41066

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 6 commits into from
Apr 22, 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
43 changes: 43 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,49 @@ def _aggregate_multiple_funcs(self, arg):
)
return self.obj._constructor_expanddim(output, columns=columns)

def _cython_agg_general(
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
):
output: dict[base.OutputKey, ArrayLike] = {}
# Ideally we would be able to enumerate self._iterate_slices and use
# the index from enumeration as the key of output, but ohlc in particular
# returns a (n x 4) array. Output requires 1D ndarrays as values, so we
# need to slice that up into 1D arrays
idx = 0
for obj in self._iterate_slices():
name = obj.name
is_numeric = is_numeric_dtype(obj.dtype)
if numeric_only and not is_numeric:
continue

result = self.grouper._cython_operation(
"aggregate", obj._values, how, axis=0, min_count=min_count
)

if how == "ohlc":
# e.g. ohlc
agg_names = ["open", "high", "low", "close"]
assert len(agg_names) == result.shape[1]
for result_column, result_name in zip(result.T, agg_names):
key = base.OutputKey(label=result_name, position=idx)
output[key] = result_column
idx += 1
else:
assert result.ndim == 1
key = base.OutputKey(label=name, position=idx)
output[key] = result
idx += 1

if not output:
raise DataError("No numeric types to aggregate")

# error: Argument 1 to "_wrap_aggregated_output" of "BaseGroupBy" has
# incompatible type "Dict[OutputKey, Union[ndarray, DatetimeArray]]";
# expected "Mapping[OutputKey, ndarray]"
return self._wrap_aggregated_output(
output, index=self.grouper.result_index # type: ignore[arg-type]
)

# TODO: index should not be Optional - see GH 35490
def _wrap_series_output(
self,
Expand Down
Loading