Skip to content

Cookbook groupby apply example is failing #8944

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions