Skip to content

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

Open
@jreback

Description

@jreback

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    ApplyApply, Aggregate, Transform, MapBugGroupby

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions