Skip to content

groupby().apply is too opinionated #3241

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
ghost opened this issue Apr 2, 2013 · 2 comments
Closed

groupby().apply is too opinionated #3241

ghost opened this issue Apr 2, 2013 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 2, 2013

apply looks at the returned values from the user-supplied transformer function
and does convenient things based on the type returned.

However, there's no opt-out if you just want to transform groups
into something and get it back as-is:

In [16]: df=mkdf(10,2,data_gen_f=lambda x,y: randint(1,10))
    ...: df
    ...: 
    ...: 
    ...: 
Out[16]: 
C0       C_l0_g0  C_l0_g1
R0                       
R_l0_g0        9        1
R_l0_g1        3        7
R_l0_g2        8        1
R_l0_g3        4        3
R_l0_g4        5        3
R_l0_g5        7        2
R_l0_g6        4        1
R_l0_g7        5        4
R_l0_g8        9        7
R_l0_g9        4        8

In [17]: def f1(g):
    ...:     return g.sort('C_l0_g0')
    ...: # group on the suffix of the running index 
    ...: g=df.groupby(lambda key: int(key.split("g")[-1]) >= 5)
    ...: r=g.apply(f1)
    ...: 

# we want to get a bunch of sorted dataframes, but get concat-ed against our will
In [18]: r
Out[18]: 
C0             C_l0_g0  C_l0_g1
      R0                       
False R_l0_g1        3        7
      R_l0_g3        4        3
      R_l0_g4        5        3
      R_l0_g2        8        1
      R_l0_g0        9        1
True  R_l0_g6        4        1
      R_l0_g9        4        8
      R_l0_g7        5        4
      R_l0_g5        7        2
      R_l0_g8        9        7

More failed guessing of what the user wants: #5908.

@jreback
Copy link
Contributor

jreback commented Sep 21, 2013

could add a raw keyword here to avoid wrapping the output

@jreback jreback modified the milestones: 0.15.0, 0.14.0 Feb 15, 2014
@jreback
Copy link
Contributor

jreback commented Jan 26, 2015

not sure what this is actually about. Lots of ways to do this anyhow.

@jreback jreback closed this as completed Jan 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant