Skip to content

BUG: SeriesGroupby.nunique raises an IndexError on empty Series #12553 #12560

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
4 changes: 4 additions & 0 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2843,6 +2843,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: # bugfix for 12553
return Series(name=self.name)

val = self.obj.get_values()

try:
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -6465,6 +6465,14 @@ def testit(label_list, shape):
testit(label_list, shape)


def test_nunique_empty_Series():
series = Series(name='foo')
group = series.groupby(level=0)
result = group.nunique()
expected = Series(name='foo')
assert_series_equal(result, expected)
Copy link
Member

Choose a reason for hiding this comment

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

Can you put this test inside the TestGroupBy class? Eg after test_nunique_with_object at Line 6404 (https://github.com/thanasis2028/pandas/blob/744e537c3c73ecb30a62a7707db138b7f4dafcd8/pandas/tests/test_groupby.py#L6400)



if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure', '-s'
], exit=False)