Skip to content
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

Sparse data frame doesn't groupby.mean() correctly #5078

Closed
langmore opened this issue Oct 2, 2013 · 2 comments · Fixed by #28425
Closed

Sparse data frame doesn't groupby.mean() correctly #5078

langmore opened this issue Oct 2, 2013 · 2 comments · Fixed by #28425
Labels
Bug Groupby Sparse Sparse Data Type

Comments

@langmore
Copy link

langmore commented Oct 2, 2013

>>>import pandas as pd
>>> pd.__version__
 '0.12.0-660-gec77315'

>>> import numpy as np
>>> np.version.version
'1.7.1'

>>> df = pd.DataFrame({'a': [0, 1, 0, 0], 'b': [0, 1, 0, 0]})
>>> sdf = df.to_sparse(fill_value=0)
>>> df.groupby('a').mean() 
    b
a   
0  0
1  1

>>> sdf.groupby('a').mean() 
    b
a   
1  0

I'm not surpised that the mean for group a == 0 was not returned. It is surprising that the result for group a == 1 was incorrect.

@OmerJog
Copy link

OmerJog commented Nov 28, 2018

Are there any planes to fix this any time soon?

@TomAugspurger
Copy link
Contributor

@OmerJog this works with a DataFrame with sparse values

In [90]: df = pd.DataFrame({'a': [0, 1, 0, 0], 'b': [0, 1, 0, 0]})

In [91]: df.apply(pd.SparseArray).groupby('a').mean()
Out[91]:
   b
a
0  0
1  1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Groupby Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants