groupby apply does not work if single value for groupby key
The following groupby fails if key1 contains only a single value.  I am using pandas 12 and python 3.x

This works:

import pandas as pd
import numpy as np

df = pd.DataFrame({'key1': ['a','a','n','a', 'a'],
                    'data1': np.random.randn(5),
                   'data2': np.random.randn(5)})

def x(df):
    return df['data2']

df['test'] = df.groupby(df['key1']).apply(x)
print (df)

This fails. Note the key1 values which are all the same.

import pandas as pd
import numpy as np

df = pd.DataFrame({'key1': ['a','a','a','a', 'a'],
                   'data1': np.random.randn(5),
                   'data2': np.random.randn(5)})

def x(df):
    return df['data2']

df['test'] = df.groupby(df['key1']).apply(x)
print (df)
