没有要聚合的数字类型-groupby()行为是否发生变化? [英] No numeric types to aggregate - change in groupby() behaviour?
问题描述
我对某些分组代码有疑问,我确信一旦运行(在较早的熊猫版本上).在0.9上,我得到没有要聚合的数字类型错误.有什么想法吗?
I have a problem with some groupy code which I'm quite sure once ran (on an older pandas version). On 0.9, I get No numeric types to aggregate errors. Any ideas?
In [31]: data
Out[31]:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2557 entries, 2004-01-01 00:00:00 to 2010-12-31 00:00:00
Freq: <1 DateOffset>
Columns: 360 entries, -89.75 to 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 to aggregate')
1458
1459 return new_blocks
DataError: No numeric types to aggregate
推荐答案
如何生成数据?
查看输出如何显示您的数据为对象"类型? groupby操作首先专门检查每一列是否为数字dtype.
See how the output shows that your data is of 'object' type? the groupby operations specifically check whether each column is a numeric dtype first.
In [31]: data
Out[31]:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2557 entries, 2004-01-01 00:00:00 to 2010-12-31 00:00:00
Freq: <1 DateOffset>
Columns: 360 entries, -89.75 to 89.75
dtypes: object(360)
外观↑
您是否先初始化了一个空的DataFrame,然后将其填充?如果是这样,这可能就是为什么它会像0.9之前的新版本一样更改的原因,将空的DataFrames初始化为浮点类型,但现在它们属于对象类型.如果是这样,您可以将初始化更改为DataFrame(dtype=float)
.
Did you initialize an empty DataFrame first and then filled it? If so that's probably why it changed with the new version as before 0.9 empty DataFrames were initialized to float type but now they are of object type. If so you can change the initialization to DataFrame(dtype=float)
.
您也可以致电frame.astype(float)
这篇关于没有要聚合的数字类型-groupby()行为是否发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!