I would like to calculate the mean
and standard deviation
of a timedelta
by bank from a dataframe
with two columns shown below. When I run the code (also shown below) I get the below error:
pandas.core.base.DataError: No numeric types to aggregate
My dataframe:
bank diff
Bank of Japan 0 days 00:00:57.416000
Reserve Bank of Australia 0 days 00:00:21.452000
Reserve Bank of New Zealand 55 days 12:39:32.269000
U.S. Federal Reserve 8 days 13:27:11.387000
My code:
means = dropped.groupby('bank').mean()
std = dropped.groupby('bank').std()
No need to convert
timedelta
back and forth. Numpy and pandas can seamlessly do it for you with a faster run time. Using yourdropped
DataFrame
:You need to convert
timedelta
to some numeric value, e.g.int64
byvalues
what is most accurate, because convert tons
is what is the numeric representation oftimedelta
:Another solution is to convert values to
seconds
bytotal_seconds
, but that is less accurate:Pandas
mean()
and other aggregation methods supportnumeric_only=False
parameter.Found here: Aggregations for Timedelta values in the Python DataFrame