1

I have the following df groupby result,

df_group = df.groupby(['code', 'year_month']).count()

                                 days_outstanding  
code      year_month                            
1            2016-10                            4  
             2016-12                            1  
             2017-01                            1

when I tried,

df_days = df.groupby(['code', 'year_month'])['days_outstanding'].mean().reset_index(name='avg_days_outanding')

to get the average days for each group for column days_outstanding, but I got the following error,

 pandas.core.base.DataError: No numeric types to aggregate

I am wondering how to fix it.

UPDATE

print(df['days_outstanding'].head().apply(type))

436     <class 'pandas._libs.tslib.Timedelta'>
437     <class 'pandas._libs.tslib.Timedelta'>
839     <class 'pandas._libs.tslib.Timedelta'>
2447    <class 'pandas._libs.tslib.Timedelta'>
3844    <class 'pandas._libs.tslib.Timedelta'>

the df is like,

       code    year_month   days_outstanding
436    1       2016-10      0
437    1       2016-10      0
839    1       2016-10      0
2447   1       2016-10      0
3844   1       2016-12      0
9157   1       2017-01      0
daiyue
  • 7,196
  • 25
  • 82
  • 149
  • I think `['days_outstanding']` is not numeric, what return `print(df['days_outstanding'].head().apply(type))` ? – jezrael Mar 22 '18 at 10:41
  • @jezrael I have edited my op to show the print result – daiyue Mar 22 '18 at 10:50
  • 1
    convert your `days_outstanding` column to integer via `df['days_outstanding'] = df['days_outstanding'].dt.days` beforehand. – jpp Mar 22 '18 at 10:59
  • 1
    @jpp that works perfectly, thx – daiyue Mar 22 '18 at 11:02
  • Possible duplicate of [Python: Convert timedelta to int in a dataframe](https://stackoverflow.com/questions/25646200/python-convert-timedelta-to-int-in-a-dataframe) – jpp Mar 22 '18 at 11:03

0 Answers0