Skip to content

Cookbook groupby apply example is failing #8944

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
jorisvandenbossche opened this issue Nov 30, 2014 · 1 comment · Fixed by #9007
Closed

Cookbook groupby apply example is failing #8944

jorisvandenbossche opened this issue Nov 30, 2014 · 1 comment · Fixed by #9007
Labels
Milestone

Comments

@jorisvandenbossche
Copy link
Member

Third example here: http://pandas-docs.github.io/pandas-docs-travis/cookbook.html#grouping

Test code:

df = pd.DataFrame({'animal': 'cat dog cat fish dog cat cat'.split(),
                    'size': list('SSMMMLL'),
                    'weight': [8, 10, 11, 1, 20, 12, 12],
                    'adult' : [False] * 5 + [True] * 2})

gb = df.groupby(['animal'])

def GrowUp(x):
    avg_weight = sum(x[x.size == 'S'].weight * 1.5)
    avg_weight += sum(x[x.size == 'M'].weight * 1.25)
    avg_weight += sum(x[x.size == 'L'].weight)
    avg_weight = avg_weight / len(x)
    return pd.Series(['L',avg_weight,True], index=['size', 'weight', 'adult'])

expected_df = gb.apply(GrowUp)

On master this gives:

In [1]: pd.__version__
Out[1]: '0.15.1-112-g2e59e42'

In [5]: expected_df = gb.apply(GrowUp)
...
KeyError: False
@jorisvandenbossche jorisvandenbossche changed the title Cookbook apply example is failing Cookbook groupby apply example is failing Nov 30, 2014
@stevesimmons
Copy link
Contributor

Joris, I have a fix for this, though messed up my commit history. What should I do for a clean PR?

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

Successfully merging a pull request may close this issue.

2 participants