Skip to content

Commit a6db652

Browse files
authored
REF: dont special-case ngroups==0 (#41331)
1 parent 1374dd1 commit a6db652

File tree

4 files changed

+9
-18
lines changed

4 files changed

+9
-18
lines changed

pandas/_libs/reduction.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ cdef class SeriesBinGrouper(_BaseGrouper):
105105
Py_ssize_t nresults, ngroups
106106

107107
cdef public:
108+
ndarray bins # ndarray[int64_t]
108109
ndarray arr, index, dummy_arr, dummy_index
109-
object values, f, bins, typ, ityp, name, idtype
110+
object values, f, typ, ityp, name, idtype
110111

111-
def __init__(self, object series, object f, object bins):
112+
def __init__(self, object series, object f, ndarray[int64_t] bins):
112113

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

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

138141
def get_result(self):

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,18 +1331,7 @@ def _agg_py_fallback(
13311331
# reductions; see GH#28949
13321332
ser = df.iloc[:, 0]
13331333

1334-
# Create SeriesGroupBy with observed=True so that it does
1335-
# not try to add missing categories if grouping over multiple
1336-
# Categoricals. This will done by later self._reindex_output()
1337-
# Doing it here creates an error. See GH#34951
1338-
sgb = get_groupby(ser, self.grouper, observed=True)
1339-
# For SeriesGroupBy we could just use self instead of sgb
1340-
1341-
if self.ngroups > 0:
1342-
res_values = self.grouper.agg_series(ser, alt)
1343-
else:
1344-
# equiv: res_values = self._python_agg_general(alt)
1345-
res_values = sgb._python_apply_general(alt, ser)._values
1334+
res_values = self.grouper.agg_series(ser, alt)
13461335

13471336
if isinstance(values, Categorical):
13481337
# Because we only get here with known dtype-preserving

pandas/core/groupby/ops.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,8 @@ def _cython_operation(
968968

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

974974
cast_back = True
975975
if len(obj) == 0:
@@ -1006,7 +1006,6 @@ def _aggregate_series_fast(self, obj: Series, func: F) -> np.ndarray:
10061006
# - obj.index is not a MultiIndex
10071007
# - obj is backed by an ndarray, not ExtensionArray
10081008
# - len(obj) > 0
1009-
# - ngroups != 0
10101009
func = com.is_builtin_func(func)
10111010

10121011
ids, _, ngroups = self.group_info

pandas/tests/groupby/test_bin_groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_series_grouper_requires_nonempty_raises():
5656
def test_series_bin_grouper():
5757
obj = Series(np.random.randn(10))
5858

59-
bins = np.array([3, 6])
59+
bins = np.array([3, 6], dtype=np.int64)
6060

6161
grouper = libreduction.SeriesBinGrouper(obj, np.mean, bins)
6262
result, counts = grouper.get_result()

0 commit comments

Comments
 (0)