Skip to content

CLN: remove unnecessary return from agg_series #41330

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
May 6, 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
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
36 changes: 19 additions & 17 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down