GroupBy.apply type upcasting regression · Issue #3911 · pandas-dev/pandas (original) (raw)

@wesm

Blocker for 0.11.1. This got broken sometime between 0.11.0 and master; I haven't had time to bisect yet. Essentially, write a mixed type DataFrame and a groupby function that extracts a row. This used to do type inference and convert object back to numeric in the columns of the resulting DataFrame. Didn't have a test so I don't blame anyone for breaking it by accident =)

In [151]: cafdata.dtypes
Out[151]:
id            int64
food         object
fgroup       object
nutrient     object
ngroup       object
units        object
value       float64
dtype: object

In [152]:
def max_value(group):
    return group.ix[group['value'].idxmax()]

max_value(cafdata)

Out[152]:
id                                      14366
food        Tea, instant, unsweetened, powder
fgroup                              Beverages
nutrient                             Caffeine
ngroup                                  Other
units                                      mg
value                                    3680
Name: 336702, dtype: object

In [153]: cafdata.groupby('fgroup').apply(max_value).dtypes
Out[153]:
id          object
food        object
fgroup      object
nutrient    object
ngroup      object
units       object
value       object
dtype: object