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

Conversation

mroeschke
Copy link
Member

Similar to #12560, but it has appeared to stall. Wanted to push it along.

@max-sixty
Copy link
Contributor

max-sixty commented Nov 30, 2016

I think nunique was one of a few cases I hadn't completed when trying to solve some of these groupby + empty series. Generally I had tried to remove empty as a special case. But if that's not possible, sobeit.

Regardless we should use shallow_copy rather than Series here

@@ -2898,6 +2898,10 @@ 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

if len(ids) == 0:
return Series(index=self.dtype.index, name=self.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

index=self.obj.index


if len(ids) == 0:
return Series(index=self.dtype.index, name=self.name)

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

…12553)

Modified tests

simplify tests

Add whatsnew

Moved len check
@mroeschke
Copy link
Member Author

Thanks @jreback, moved the len check down the stack. Just waiting for Travis to finish the build.

@jreback jreback added this to the 0.19.2 milestone Dec 4, 2016
@jreback jreback closed this in c0e13d1 Dec 4, 2016
@jreback
Copy link
Contributor

jreback commented Dec 4, 2016

thanks @mroeschke

jorisvandenbossche pushed a commit that referenced this pull request Dec 15, 2016
@mroeschke mroeschke deleted the fix_12553 branch December 20, 2017 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SeriesGroupby.nunique raises an IndexError on empty Series
3 participants