1

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

stovfl
  • 14,998
  • 7
  • 24
  • 51
Elise1369
  • 259
  • 1
  • 6
  • 19
  • When I try : `pd._to_datetime(df_abc).dt.mean()`, I get the following error : AttributeError: module 'pandas' has no attribute '_to_datetime' – Elise1369 Jan 14 '19 at 10:46
  • Sorry, I'm stupid and didn't have a real look to the error ^^ So I rather have this error : TypeError: is not convertible to datetime – Elise1369 Jan 14 '19 at 10:53
  • https://stackoverflow.com/questions/44616546/finding-the-mean-and-standard-deviation-of-a-timedelta-object-in-pandas-df – Btc Sources Jan 14 '19 at 10:59
  • I finally solved my problem with https://stackoverflow.com/questions/51101432/timedelta-to-string-type-in-pandas-dataframe. Thank you for your help ! – Elise1369 Jan 14 '19 at 12:18

0 Answers0