I try to convert timedelta into datetime to get then an excel with hh:mm:ss
format, but I don't achieve this.
I have an extract of dataframe df_abc
that looks like this (the data is a timedelta
):
0 days 01:20:34
0 days 01:20:24
0 days 01:20:24
0 days 01:20:23
My goal is to compute the mean of these data and to save it on an excel file with hh:mm:ss
format.
When I save dates like 0 days 01:20:34
on excel, it writes only a number of days (ex : 0.05
, instead of 01:20:34
).
I achieved to convert it to datetime by using :
pd.to_datetime(df_abc).dt.time
But to compute the mean of these durations:
When I try
pd.to_datetime(df_abc).dt.time.mean()
it gives this error:
TypeError: unsupported operand type(s) for +: 'datetime.time' and 'datetime.time'
When I try
df_abc.mean()
it gives this error:
TypeError: Could not convert 0 days 05:21:45 to numeric
I solved it by using : mean_df_abc = pd.to_timedelta(df_abc).mean()
.
=> So, now I want to convert : Timedelta('0 days 01:20:26.250000')
into datetime, because timedelta
format can't be printed on excel like hh:mm:ss
.
pd.to_datetime(mean_df_abc).dt.time
provides this error:
TypeError: <class 'pandas._libs.tslibs.timedeltas.Timedelta'> is not convertible to datetime
Have you an idea of what can be done to solve this problem ? (perhaps another way to save on excel with hh:mm:ss
format ?)