Skip to content

REF: dont special-case ngroups==0 #41331

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 3 commits 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
7 changes: 5 additions & 2 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ cdef class SeriesBinGrouper(_BaseGrouper):
Py_ssize_t nresults, ngroups

cdef public:
ndarray bins # ndarray[int64_t]
ndarray arr, index, dummy_arr, dummy_index
object values, f, bins, typ, ityp, name, idtype
object values, f, typ, ityp, name, idtype

def __init__(self, object series, object f, object bins):
def __init__(self, object series, object f, ndarray[int64_t] bins):

assert len(bins) > 0 # otherwise we get IndexError in get_result

Expand All @@ -133,6 +134,8 @@ cdef class SeriesBinGrouper(_BaseGrouper):
if len(bins) > 0 and bins[-1] == len(series):
self.ngroups = len(bins)
else:
# TODO: not reached except in test_series_bin_grouper directly
# constructing SeriesBinGrouper; can we rule this case out?
self.ngroups = len(bins) + 1

def get_result(self):
Expand Down
13 changes: 1 addition & 12 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,18 +1331,7 @@ def _agg_py_fallback(
# reductions; see GH#28949
ser = df.iloc[:, 0]

# Create SeriesGroupBy with observed=True so that it does
# not try to add missing categories if grouping over multiple
# Categoricals. This will done by later self._reindex_output()
# Doing it here creates an error. See GH#34951
sgb = get_groupby(ser, self.grouper, observed=True)
# For SeriesGroupBy we could just use self instead of sgb

if self.ngroups > 0:
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
res_values = self.grouper.agg_series(ser, alt)

if isinstance(values, Categorical):
# Because we only get here with known dtype-preserving
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,8 @@ def _cython_operation(

@final
def agg_series(self, obj: Series, func: F) -> ArrayLike:
# Caller is responsible for checking ngroups != 0
assert self.ngroups != 0
# test_groupby_empty_with_category gets here with self.ngroups == 0
# and len(obj) > 0

cast_back = True
if len(obj) == 0:
Expand Down Expand Up @@ -1007,7 +1007,6 @@ def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray:
# - obj.index is not a MultiIndex
# - obj is backed by an ndarray, not ExtensionArray
# - len(obj) > 0
# - ngroups != 0
func = com.is_builtin_func(func)

ids, _, ngroups = self.group_info
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_bin_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_series_grouper_requires_nonempty_raises():
def test_series_bin_grouper():
obj = Series(np.random.randn(10))

bins = np.array([3, 6])
bins = np.array([3, 6], dtype=np.int64)

grouper = libreduction.SeriesBinGrouper(obj, np.mean, bins)
result, counts = grouper.get_result()
Expand Down