Skip to content

Commit 2d4ab97

Browse files
author
tp
committed
fix bug where np.bincount default arg minlength must be None for np<1.13
1 parent ce87d06 commit 2d4ab97

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v0.23.4.txt

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bug Fixes
3131
**Groupby/Resample/Rolling**
3232

3333
- Bug where calling :func:`DataFrameGroupBy.agg` with a list of functions including ``ohlc`` as the non-initial element would raise a ``ValueError`` (:issue:`21716`)
34+
- Bug in :func:`pandas.core.groupby.Series GroupBy.count` when using numpy < 1.13 and ngroups=0 (:issue:`21956`).
3435
-
3536

3637
**Conversion**

pandas/core/groupby/generic.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from pandas.core.index import Index, MultiIndex, CategoricalIndex
4747
from pandas.core.arrays.categorical import Categorical
4848
from pandas.core.internals import BlockManager, make_block
49+
from pandas.compat.numpy import _np_version_under1p13
4950

5051
from pandas.plotting._core import boxplot_frame_groupby
5152

@@ -1207,7 +1208,10 @@ def count(self):
12071208

12081209
mask = (ids != -1) & ~isna(val)
12091210
ids = ensure_platform_int(ids)
1210-
out = np.bincount(ids[mask], minlength=ngroups or 0)
1211+
minlength = ngroups or 0
1212+
if _np_version_under1p13 and minlength == 0:
1213+
minlength = None
1214+
out = np.bincount(ids[mask], minlength=minlength)
12111215

12121216
return Series(out,
12131217
index=self.grouper.result_index,

pandas/tests/groupby/test_counting.py

+10
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,13 @@ def test_count_with_datetimelike(self, datetimelike):
212212
expected = DataFrame({'y': [2, 1]}, index=['a', 'b'])
213213
expected.index.name = "x"
214214
assert_frame_equal(expected, res)
215+
216+
def test_count_with_only_nans_in_first_group(self):
217+
# GH21956
218+
df = DataFrame({'A': [np.nan, np.nan], 'B': ['a', 'b'], 'C': [1, 2]})
219+
result = df.groupby(['A', 'B']).C.count()
220+
mi = MultiIndex(levels=[[], ['a', 'b']],
221+
labels=[[], []],
222+
names=['A', 'B'])
223+
expected = Series([], index=mi, dtype=np.int64, name='C')
224+
assert_series_equal(result, expected, check_index_type=False)

0 commit comments

Comments
 (0)