Skip to content

Commit f5e0e6c

Browse files
jbrockmendelyeshsurya
authored andcommitted
REF: BaseGroupBy methods belong in GroupBy (pandas-dev#41066)
1 parent d205f22 commit f5e0e6c

File tree

3 files changed

+544
-511
lines changed

3 files changed

+544
-511
lines changed

pandas/core/groupby/generic.py

+43
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,49 @@ def _aggregate_multiple_funcs(self, arg):
345345
)
346346
return self.obj._constructor_expanddim(output, columns=columns)
347347

348+
def _cython_agg_general(
349+
self, how: str, alt=None, numeric_only: bool = True, min_count: int = -1
350+
):
351+
output: dict[base.OutputKey, ArrayLike] = {}
352+
# Ideally we would be able to enumerate self._iterate_slices and use
353+
# the index from enumeration as the key of output, but ohlc in particular
354+
# returns a (n x 4) array. Output requires 1D ndarrays as values, so we
355+
# need to slice that up into 1D arrays
356+
idx = 0
357+
for obj in self._iterate_slices():
358+
name = obj.name
359+
is_numeric = is_numeric_dtype(obj.dtype)
360+
if numeric_only and not is_numeric:
361+
continue
362+
363+
result = self.grouper._cython_operation(
364+
"aggregate", obj._values, how, axis=0, min_count=min_count
365+
)
366+
367+
if how == "ohlc":
368+
# e.g. ohlc
369+
agg_names = ["open", "high", "low", "close"]
370+
assert len(agg_names) == result.shape[1]
371+
for result_column, result_name in zip(result.T, agg_names):
372+
key = base.OutputKey(label=result_name, position=idx)
373+
output[key] = result_column
374+
idx += 1
375+
else:
376+
assert result.ndim == 1
377+
key = base.OutputKey(label=name, position=idx)
378+
output[key] = result
379+
idx += 1
380+
381+
if not output:
382+
raise DataError("No numeric types to aggregate")
383+
384+
# error: Argument 1 to "_wrap_aggregated_output" of "BaseGroupBy" has
385+
# incompatible type "Dict[OutputKey, Union[ndarray, DatetimeArray]]";
386+
# expected "Mapping[OutputKey, ndarray]"
387+
return self._wrap_aggregated_output(
388+
output, index=self.grouper.result_index # type: ignore[arg-type]
389+
)
390+
348391
# TODO: index should not be Optional - see GH 35490
349392
def _wrap_series_output(
350393
self,

0 commit comments

Comments
 (0)