Skip to content

BUG: Error upon Series.Groupby.nunique with empty Series (#12553) #14770

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.19.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Bug Fixes
- Bug in resampling a ``DatetimeIndex`` in local TZ, covering a DST change, which would raise ``AmbiguousTimeError`` (:issue:`14682`)



- Bug in ``Series.groupby.nunique()`` raising an ``IndexError`` for an empty ``Series`` (:issue:`12553`)



Expand Down
6 changes: 5 additions & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2898,6 +2898,7 @@ def true_and_notnull(x, *args, **kwargs):
def nunique(self, dropna=True):
""" Returns number of unique elements in the group """
ids, _, _ = self.grouper.group_info

val = self.obj.get_values()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actualy I'd rather you fix like this:

diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py
index afddb86..f720a62 100644
--- a/pandas/core/groupby.py
+++ b/pandas/core/groupby.py
@@ -2928,7 +2928,10 @@ class SeriesGroupBy(GroupBy):
             inc[idx] = 1
 
         out = np.add.reduceat(inc, idx).astype('int64', copy=False)
-        res = out if ids[0] != -1 else out[1:]
+        if len(ids):
+            res = out if ids[0] != -1 else out[1:]
+        else:
+            res = out[1:]
         ri = self.grouper.result_index
 
         # we might have duplications among the bins

try:
Expand Down Expand Up @@ -2928,7 +2929,10 @@ def nunique(self, dropna=True):
inc[idx] = 1

out = np.add.reduceat(inc, idx).astype('int64', copy=False)
res = out if ids[0] != -1 else out[1:]
if len(ids):
res = out if ids[0] != -1 else out[1:]
else:
res = out[1:]
ri = self.grouper.result_index

# we might have duplications among the bins
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -6754,6 +6754,13 @@ def test_nunique_with_object(self):
expected = pd.Series([1] * 5, name='name', index=index)
tm.assert_series_equal(result, expected)

def test_nunique_with_empty_series(self):
# GH 12553
data = pd.Series(name='name')
result = data.groupby(level=0).nunique()
expected = pd.Series(name='name', dtype='int64')
tm.assert_series_equal(result, expected)

def test_transform_with_non_scalar_group(self):
# GH 10165
cols = pd.MultiIndex.from_tuples([
Expand Down