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