BUG: Bug upon Series.Groupby.nunique with empty Series
closes #12553
closes #14770
(cherry picked from commit c0e13d1bcc
)
This commit is contained in:
parent
04b83e021b
commit
36dad8418f
|
@ -59,6 +59,8 @@ Bug Fixes
|
|||
|
||||
|
||||
- Bug ``HDFStore`` writing a ``MultiIndex`` when using ``data_columns=True`` (:issue:`14435`)
|
||||
- Bug in ``Series.groupby.nunique()`` raising an ``IndexError`` for an empty ``Series`` (:issue:`12553`)
|
||||
|
||||
|
||||
|
||||
- Bug in clipboard functions on linux with python2 with unicode and separators (:issue:`13747`)
|
||||
|
|
|
@ -2908,6 +2908,7 @@ class SeriesGroupBy(GroupBy):
|
|||
def nunique(self, dropna=True):
|
||||
""" Returns number of unique elements in the group """
|
||||
ids, _, _ = self.grouper.group_info
|
||||
|
||||
val = self.obj.get_values()
|
||||
|
||||
try:
|
||||
|
@ -2938,7 +2939,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
|
||||
|
|
|
@ -6773,6 +6773,13 @@ class TestGroupBy(tm.TestCase):
|
|||
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([
|
||||
|
|
Loading…
Reference in New Issue