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

API: groupby-apply with on a DataFrame returning a Series can have misleading inference #20665

Open
jreback opened this issue Apr 12, 2018 · 0 comments
Labels
Apply Apply, Aggregate, Transform Bug Groupby

Comments

@jreback
Copy link
Contributor

jreback commented Apr 12, 2018

xref #3599. Should prob not do this kind of inference.

In [83]: df1 = DataFrame([{"val1": 1, "val2" : 20}, {"val1":1, "val2": 19}, 
    ...:                  {"val1":2, "val2": 27}, {"val1":2, "val2": 12}])
    ...:                  

In [84]: df2 = DataFrame([{"val1": 1, "val2" : 20}, {"val1":1, "val2": 19}, 
    ...:                  {"val1":1, "val2": 27}, {"val1":1, "val2": 12}])
    ...:                  

In [85]: df1.groupby('val1').apply(lambda x: x.val2-x.val2.mean())
Out[85]: 
val1   
1     0    0.5
      1   -0.5
2     2    7.5
      3   -7.5
Name: val2, dtype: float64

In [86]: df2.groupby('val1').apply(lambda x: x.val2-x.val2.mean())
Out[86]: 
val2    0    1    2    3
val1                    
1     0.5 -0.5  7.5 -7.5

In [87]: df2.groupby('val1', squeeze=True).apply(lambda x: x.val2-x.val2.mean())
Out[87]: 
0    0.5
1   -0.5
2    7.5
3   -7.5
Name: 1, dtype: float64

[87] should a) have the correct index
but this should just work w/o the squeeze kwarg

@jreback jreback added Groupby Reshaping Concat, Merge/Join, Stack/Unstack, Explode API Design Difficulty Intermediate labels Apr 12, 2018
@jreback jreback added this to the Next Major Release milestone Apr 12, 2018
@mroeschke mroeschke added Apply Apply, Aggregate, Transform Bug and removed API Design Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Jun 19, 2021
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Apply Apply, Aggregate, Transform Bug Groupby
Projects
No open projects
Interesting Things
Awaiting triage
Development

No branches or pull requests

3 participants