No numeric types to aggregate - change in groupby() behaviour?

前端 未结 2 540
借酒劲吻你
借酒劲吻你 2020-12-03 04:06

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?

相关标签:
2条回答
  • 2020-12-03 04:43

    How are you generating your data?

    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)
    

    look ↑


    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).

    You can also call frame.astype(float)

    0 讨论(0)
  • 2020-12-03 04:51

    I got this error generating a data frame consisting of timestamps and data:

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

    Adding the suggested solution works for me:

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

    Thanks Chang She!

    Example:

                         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
    
    0 讨论(0)
提交回复
热议问题