【问题标题】:No numeric types to aggregate - change in groupby() behaviour?没有要聚合的数字类型 - groupby() 行为的变化?
【发布时间】:2012-10-02 09:51:46
【问题描述】:

我很确定曾经运行过一些分组代码(在较旧的 pandas 版本上)有问题。在 0.9 上,我得到 No numeric types to aggregate 错误。有什么想法吗?

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

【问题讨论】:

  • 对于那些在这里结束的人,.apply(pd.to_numeric) DataFrame 方法可能会有所帮助。
  • 我确实来到了这里,@Foad,谢谢。我可以在昨天运行的代码上计算 .sum() 和 .count(),但不能计算 .mean()。坚持.apply(pd.to_numeric) 修复它。

标签: python pandas


【解决方案1】:

您是如何生成数据的?

查看输出如何显示您的数据属于“对象”类型? groupby 操作首先专门检查每一列是否是数字 dtype。

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)

您也可以拨打frame.astype(float)

【讨论】:

  • 很奇怪,那么如何对包含字符串的字段进行分组呢?这似乎更适用于标量分组,而不是基于列中的值。
  • @dcsan 错误消息有点误导。您可以聚合非数字类型,使用 first() 和pandas.pydata.org/pandas-docs/stable/user_guide/… 的类似方法。错误消息在尝试进行数字聚合时特别触发,例如意思()
【解决方案2】:

生成包含时间戳和数据的数据框时出现此错误:

df = pd.DataFrame({'data':value}, index=pd.DatetimeIndex(timestamp))

添加建议的解决方案对我有用:

df = pd.DataFrame({'data':value}, index=pd.DatetimeIndex(timestamp), dtype=float))

谢谢长舍!

例子:

                     data
2005-01-01 00:10:00  7.53
2005-01-01 00:20:00  7.54
2005-01-01 00:30:00  7.62
2005-01-01 00:40:00  7.68
2005-01-01 00:50:00  7.81
2005-01-01 01:00:00  7.95
2005-01-01 01:10:00  7.96
2005-01-01 01:20:00  7.95
2005-01-01 01:30:00  7.98
2005-01-01 01:40:00  8.06
2005-01-01 01:50:00  8.04
2005-01-01 02:00:00  8.06
2005-01-01 02:10:00  8.12
2005-01-01 02:20:00  8.12
2005-01-01 02:30:00  8.25
2005-01-01 02:40:00  8.27
2005-01-01 02:50:00  8.17
2005-01-01 03:00:00  8.21
2005-01-01 03:10:00  8.29
2005-01-01 03:20:00  8.31
2005-01-01 03:30:00  8.25
2005-01-01 03:40:00  8.19
2005-01-01 03:50:00  8.17
2005-01-01 04:00:00  8.18
                     data
2005-01-01 00:00:00  7.636000
2005-01-01 01:00:00  7.990000
2005-01-01 02:00:00  8.165000
2005-01-01 03:00:00  8.236667
2005-01-01 04:00:00  8.180000

【讨论】:

    【解决方案3】:

    我是通过以下方式完成的:

    data_frame.groupby(COL1).COL2.apply(np.mean).reset_index()
    

    【讨论】:

      【解决方案4】:

      在这里遇到了同样的问题,搜索了很长时间才意识到我的值不是浮点数而是字符串。

      这是解决我的问题的方法:

      df["column_name"] = pd.to_numeric(df["column_name"], downcast="float")
      

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 1970-01-01
        • 2020-09-02
        • 1970-01-01
        • 2021-09-22
        • 2017-01-06
        • 2020-04-02
        • 2019-06-23
        • 1970-01-01
        相关资源
        最近更新 更多