From 744e537c3c73ecb30a62a7707db138b7f4dafcd8 Mon Sep 17 00:00:00 2001 From: thanasis Date: Wed, 1 Jun 2016 21:12:32 +0300 Subject: [PATCH] BUG: Fix for issue #12553 Author: Thanasis Katsios --- pandas/core/groupby.py | 4 ++++ pandas/tests/test_groupby.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index bea62e98e4a2a..0cf01a0eba71e 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -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: diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index 6659e6b106a67..7e8d2869f574c 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -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) + + if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure', '-s' ], exit=False)