Skip to content

Commit 664ddb8

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

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

pandas/core/groupby/generic.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from pandas.core.index import Index, MultiIndex, CategoricalIndex
4646
from pandas.core.arrays.categorical import Categorical
4747
from pandas.core.internals import BlockManager, make_block
48+
from pandas.compat.numpy import _np_version_under1p13
4849

4950
from pandas.plotting._core import boxplot_frame_groupby
5051

@@ -1206,7 +1207,10 @@ def count(self):
12061207

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

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

pandas/tests/groupby/test_counting.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,14 @@ 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+
# GH
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)
225+

0 commit comments

Comments
 (0)