Skip to content

nunique Error when called by groupby from an empty dataframe #13239

@zhangyingmath

Description

@zhangyingmath

Initialize a dataframe: (I am using pandas version 0.17.1)

import pandas as pd
a=pd.DataFrame({'f':[1,2,3,4]})

First if I call nunique from an empty dataframe, I get what I expect:

a[a['f']>10]['f'].nunique()
0

But If I call nunique from groupby, I get an error:

a[a['f']>10].groupby('f')['f'].nunique()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/ci1/plus/tc10b/sles11sp2_gcc-4.8.3_x86-64/PythonModules-2.7.8/20160310/lib/python/pandas/core/groupby.py", line 2695, in nunique
    return Series(out if ids[0] != -1 else out[1:],
IndexError: index 0 is out of bounds for axis 0 with size 0

On the other hand, the 'unique' function doesn't have this issue:

a[a['f']>10]['f'].unique()
array([], dtype=int64)

a[a['f']>10].groupby('f')['f'].unique()
Series([], Name: f, dtype: float64)

I would like the 'nunique' function to behave like 'unique' when handling an empty dataframe + groupby. (Or please let me know if this issue is already fixed in later versions.) Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDuplicate ReportDuplicate issue or pull requestGroupbyIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions