-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
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.
|
@@ -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) |
There was a problem hiding this comment.
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() | ||
|
There was a problem hiding this comment.
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
Thanks @jreback, moved the |
thanks @mroeschke |
git diff upstream/master | flake8 --diff
Similar to #12560, but it has appeared to stall. Wanted to push it along.