diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index b7254ffecb2bc..c5ef18c51a533 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1269,7 +1269,7 @@ def _python_agg_general(self, func, *args, **kwargs): try: # if this function is invalid for this dtype, we will ignore it. - result, counts = self.grouper.agg_series(obj, f) + result = self.grouper.agg_series(obj, f) except TypeError: continue @@ -1339,7 +1339,7 @@ def _agg_py_fallback( # For SeriesGroupBy we could just use self instead of sgb if self.ngroups > 0: - res_values, _ = self.grouper.agg_series(ser, alt) + res_values = self.grouper.agg_series(ser, alt) else: # equiv: res_values = self._python_agg_general(alt) res_values = sgb._python_apply_general(alt, ser)._values diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 60d79718cd85f..a6f2a537375a4 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -969,28 +969,28 @@ def _cython_operation( ) @final - def agg_series(self, obj: Series, func: F) -> tuple[ArrayLike, np.ndarray]: + def agg_series(self, obj: Series, func: F) -> ArrayLike: # Caller is responsible for checking ngroups != 0 assert self.ngroups != 0 cast_back = True if len(obj) == 0: # SeriesGrouper would raise if we were to call _aggregate_series_fast - result, counts = self._aggregate_series_pure_python(obj, func) + result = self._aggregate_series_pure_python(obj, func) elif is_extension_array_dtype(obj.dtype): # _aggregate_series_fast would raise TypeError when # calling libreduction.Slider # In the datetime64tz case it would incorrectly cast to tz-naive # TODO: can we get a performant workaround for EAs backed by ndarray? - result, counts = self._aggregate_series_pure_python(obj, func) + result = self._aggregate_series_pure_python(obj, func) elif obj.index._has_complex_internals: # Preempt TypeError in _aggregate_series_fast - result, counts = self._aggregate_series_pure_python(obj, func) + result = self._aggregate_series_pure_python(obj, func) else: - result, counts = self._aggregate_series_fast(obj, func) + result = self._aggregate_series_fast(obj, func) cast_back = False npvalues = lib.maybe_convert_objects(result, try_float=False) @@ -999,11 +999,11 @@ def agg_series(self, obj: Series, func: F) -> tuple[ArrayLike, np.ndarray]: out = maybe_cast_pointwise_result(npvalues, obj.dtype, numeric_only=True) else: out = npvalues - return out, counts + return out + + def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray: + # -> np.ndarray[object] - 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 @@ -1018,11 +1018,12 @@ def _aggregate_series_fast( obj = obj.take(indexer) ids = ids.take(indexer) sgrouper = libreduction.SeriesGrouper(obj, func, ids, ngroups) - result, counts = sgrouper.get_result() - return result, counts + result, _ = sgrouper.get_result() + return result @final - def _aggregate_series_pure_python(self, obj: Series, func: F): + def _aggregate_series_pure_python(self, obj: Series, func: F) -> np.ndarray: + # -> np.ndarray[object] ids, _, ngroups = self.group_info counts = np.zeros(ngroups, dtype=int) @@ -1047,7 +1048,7 @@ def _aggregate_series_pure_python(self, obj: Series, func: F): counts[i] = group.shape[0] result[i] = res - return result, counts + return result class BinGrouper(BaseGrouper): @@ -1205,16 +1206,17 @@ def groupings(self) -> list[grouper.Grouping]: ping = grouper.Grouping(lev, lev, in_axis=False, level=None, name=lev.name) return [ping] - def _aggregate_series_fast( - self, obj: Series, func: F - ) -> tuple[ArrayLike, np.ndarray]: + def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray: + # -> np.ndarray[object] + # 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 sbg = libreduction.SeriesBinGrouper(obj, func, self.bins) - return sbg.get_result() + result, _ = sbg.get_result() + return result def _is_indexed_like(obj, axes, axis: int) -> bool: