Skip to content

BUG: work around for np.bincount with minlength=0 #11709

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 1 commit into from
Nov 29, 2015
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Performance Improvements
Bug Fixes
~~~~~~~~~

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


- Bug in ``.loc`` against ``CategoricalIndex`` may result in normal ``Index`` (:issue:`11586`)
3 changes: 2 additions & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,8 @@ def value_counts(self, dropna=True):
ix, clean = np.arange(ncat), mask.all()

if dropna or clean:
count = bincount(code if clean else code[mask], minlength=ncat)
obs = code if clean else code[mask]
count = bincount(obs, minlength=ncat or None)
else:
count = bincount(np.where(mask, code, ncat))
ix = np.append(ix, -1)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ def size(self):
"""
ids, _, ngroup = self.group_info
ids = com._ensure_platform_int(ids)
out = np.bincount(ids[ids != -1], minlength=ngroup)
out = np.bincount(ids[ids != -1], minlength=ngroup or None)
return Series(out, index=self.result_index, dtype='int64')

@cache_readonly
Expand Down Expand Up @@ -2822,7 +2822,7 @@ def count(self):

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

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

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,8 @@ def count(self, level=None):
lab[mask] = cnt = len(lev)
lev = lev.insert(cnt, _get_na_value(lev.dtype.type))

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

def mode(self):
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,11 @@ def test_size(self):
right = df.groupby(key, sort=sort)['c'].apply(lambda a: a.shape[0])
assert_series_equal(left, right, check_names=False)

# GH11699
df = DataFrame([], columns=['A', 'B'])
out = Series([], dtype='int64', index=Index([], name='A'))
assert_series_equal(df.groupby('A').size(), out)

def test_count(self):
from string import ascii_lowercase
n = 1 << 15
Expand Down