Skip to content

Commit 36dad84

Browse files
mroeschkejorisvandenbossche
authored andcommitted
BUG: Bug upon Series.Groupby.nunique with empty Series
closes #12553 closes #14770 (cherry picked from commit c0e13d1)
1 parent 04b83e0 commit 36dad84

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.19.2.txt

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Bug Fixes
5959

6060

6161
- Bug ``HDFStore`` writing a ``MultiIndex`` when using ``data_columns=True`` (:issue:`14435`)
62+
- Bug in ``Series.groupby.nunique()`` raising an ``IndexError`` for an empty ``Series`` (:issue:`12553`)
63+
6264

6365

6466
- Bug in clipboard functions on linux with python2 with unicode and separators (:issue:`13747`)

pandas/core/groupby.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,7 @@ def true_and_notnull(x, *args, **kwargs):
29082908
def nunique(self, dropna=True):
29092909
""" Returns number of unique elements in the group """
29102910
ids, _, _ = self.grouper.group_info
2911+
29112912
val = self.obj.get_values()
29122913

29132914
try:
@@ -2938,7 +2939,10 @@ def nunique(self, dropna=True):
29382939
inc[idx] = 1
29392940

29402941
out = np.add.reduceat(inc, idx).astype('int64', copy=False)
2941-
res = out if ids[0] != -1 else out[1:]
2942+
if len(ids):
2943+
res = out if ids[0] != -1 else out[1:]
2944+
else:
2945+
res = out[1:]
29422946
ri = self.grouper.result_index
29432947

29442948
# we might have duplications among the bins

pandas/tests/test_groupby.py

+7
Original file line numberDiff line numberDiff line change
@@ -6773,6 +6773,13 @@ def test_nunique_with_object(self):
67736773
expected = pd.Series([1] * 5, name='name', index=index)
67746774
tm.assert_series_equal(result, expected)
67756775

6776+
def test_nunique_with_empty_series(self):
6777+
# GH 12553
6778+
data = pd.Series(name='name')
6779+
result = data.groupby(level=0).nunique()
6780+
expected = pd.Series(name='name', dtype='int64')
6781+
tm.assert_series_equal(result, expected)
6782+
67766783
def test_transform_with_non_scalar_group(self):
67776784
# GH 10165
67786785
cols = pd.MultiIndex.from_tuples([

0 commit comments

Comments
 (0)