python - No numeric types to aggregate - change in groupby() behaviour? -
i have problem groupy code i'm quite sure once ran (on older pandas version). on 0.9, no numeric types aggregate errors. ideas?
in [31]: data out[31]: <class 'pandas.core.frame.dataframe'> datetimeindex: 2557 entries, 2004-01-01 00:00:00 2010-12-31 00:00:00 freq: <1 dateoffset> columns: 360 entries, -89.75 89.75 dtypes: object(360) in [32]: latedges = linspace(-90., 90., 73) in [33]: lats_new = linspace(-87.5, 87.5, 72) in [34]: def _get_gridbox_label(x, bins, labels): ....: return labels[searchsorted(bins, x) - 1] ....: in [35]: lat_bucket = lambda x: _get_gridbox_label(x, latedges, lats_new) in [36]: data.t.groupby(lat_bucket).mean() --------------------------------------------------------------------------- dataerror traceback (most recent call last) <ipython-input-36-ed9c538ac526> in <module>() ----> 1 data.t.groupby(lat_bucket).mean() /usr/lib/python2.7/site-packages/pandas/core/groupby.py in mean(self) 295 """ 296 try: --> 297 return self._cython_agg_general('mean') 298 except dataerror: 299 raise /usr/lib/python2.7/site-packages/pandas/core/groupby.py in _cython_agg_general(self, how, numeric_only) 1415 1416 def _cython_agg_general(self, how, numeric_only=true): -> 1417 new_blocks = self._cython_agg_blocks(how, numeric_only=numeric_only) 1418 return self._wrap_agged_blocks(new_blocks) 1419 /usr/lib/python2.7/site-packages/pandas/core/groupby.py in _cython_agg_blocks(self, how, numeric_only) 1455 1456 if len(new_blocks) == 0: -> 1457 raise dataerror('no numeric types aggregate') 1458 1459 return new_blocks dataerror: no numeric types aggregate
how generating data?
see how output shows data of 'object' type? groupby operations check whether each column numeric dtype first.
in [31]: data out[31]: <class 'pandas.core.frame.dataframe'> datetimeindex: 2557 entries, 2004-01-01 00:00:00 2010-12-31 00:00:00 freq: <1 dateoffset> columns: 360 entries, -89.75 89.75 dtypes: object(360)
look ↑
did initialize empty dataframe first , filled it? if that's why changed new version before 0.9 empty dataframes initialized float type of object type. if can change initialization dataframe(dtype=float)
.
you can call frame.astype(float)
Comments
Post a Comment