Skip to content

Commit 92cd41a

Browse files
committed
BUG: workaround for np.bincount with minlength=0
1 parent 44cc8e5 commit 92cd41a

File tree

5 files changed

+12
-4
lines changed

5 files changed

+12
-4
lines changed

doc/source/whatsnew/v0.18.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Performance Improvements
106106
Bug Fixes
107107
~~~~~~~~~
108108

109+
- Bug in ``GroupBy.size`` when data-frame is empty. (:issue:`11699`)
109110

110111

111112
- Bug in ``.loc`` against ``CategoricalIndex`` may result in normal ``Index`` (:issue:`11586`)

pandas/core/categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,8 @@ def value_counts(self, dropna=True):
10951095
ix, clean = np.arange(ncat), mask.all()
10961096

10971097
if dropna or clean:
1098-
count = bincount(code if clean else code[mask], minlength=ncat)
1098+
obs = code if clean else code[mask]
1099+
count = bincount(obs, minlength=ncat or None)
10991100
else:
11001101
count = bincount(np.where(mask, code, ncat))
11011102
ix = np.append(ix, -1)

pandas/core/groupby.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ def size(self):
14391439
"""
14401440
ids, _, ngroup = self.group_info
14411441
ids = com._ensure_platform_int(ids)
1442-
out = np.bincount(ids[ids != -1], minlength=ngroup)
1442+
out = np.bincount(ids[ids != -1], minlength=ngroup or None)
14431443
return Series(out, index=self.result_index, dtype='int64')
14441444

14451445
@cache_readonly
@@ -2822,7 +2822,7 @@ def count(self):
28222822

28232823
mask = (ids != -1) & ~isnull(val)
28242824
ids = com._ensure_platform_int(ids)
2825-
out = np.bincount(ids[mask], minlength=ngroups) if ngroups != 0 else []
2825+
out = np.bincount(ids[mask], minlength=ngroups or None)
28262826

28272827
return Series(out, index=self.grouper.result_index, name=self.name, dtype='int64')
28282828

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,8 @@ def count(self, level=None):
11421142
lab[mask] = cnt = len(lev)
11431143
lev = lev.insert(cnt, _get_na_value(lev.dtype.type))
11441144

1145-
out = np.bincount(lab[notnull(self.values)], minlength=len(lev))
1145+
obs = lab[notnull(self.values)]
1146+
out = np.bincount(obs, minlength=len(lev) or None)
11461147
return self._constructor(out, index=lev, dtype='int64').__finalize__(self)
11471148

11481149
def mode(self):

pandas/tests/test_groupby.py

+5
Original file line numberDiff line numberDiff line change
@@ -2525,6 +2525,11 @@ def test_size(self):
25252525
right = df.groupby(key, sort=sort)['c'].apply(lambda a: a.shape[0])
25262526
assert_series_equal(left, right, check_names=False)
25272527

2528+
# GH11699
2529+
df = DataFrame([], columns=['A', 'B'])
2530+
out = Series([], dtype='int64', index=Index([], name='A'))
2531+
assert_series_equal(df.groupby('A').size(), out)
2532+
25282533
def test_count(self):
25292534
from string import ascii_lowercase
25302535
n = 1 << 15

0 commit comments

Comments
 (0)