Skip to content

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

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
zhangyingmath opened this issue May 20, 2016 · 1 comment
Closed

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

zhangyingmath opened this issue May 20, 2016 · 1 comment
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@zhangyingmath
Copy link

zhangyingmath commented May 20, 2016

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!

@jreback
Copy link
Contributor

jreback commented May 20, 2016

this is a dupe of #12553

@jreback jreback closed this as completed May 20, 2016
@jreback jreback added Bug Groupby Indexing Related to indexing on series/frames, not to indexes themselves Duplicate Report Duplicate issue or pull request labels May 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Groupby Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

2 participants