timedelta to string type in pandas dataframe timedelta to string type in pandas dataframe pandas pandas

timedelta to string type in pandas dataframe


It is possible by:

df['duration1'] = df['duration'].astype(str).str[-18:-10]

But solution is not general, if input is 3 days 05:01:11 it remove 3 days too.

So solution working only for timedeltas less as one day correctly.

More general solution is create custom format:

N = 10np.random.seed(11230)rng = pd.date_range('2017-04-03 15:30:00', periods=N, freq='13.5H')df = pd.DataFrame({'duration': np.abs(np.random.choice(rng, size=N) -                                  np.random.choice(rng, size=N)) })  df['duration1'] = df['duration'].astype(str).str[-18:-10]def f(x):    ts = x.total_seconds()    hours, remainder = divmod(ts, 3600)    minutes, seconds = divmod(remainder, 60)    return ('{}:{:02d}:{:02d}').format(int(hours), int(minutes), int(seconds)) df['duration2'] = df['duration'].apply(f)print (df)         duration duration1  duration20 2 days 06:00:00  06:00:00   54:00:001 2 days 19:30:00  19:30:00   67:30:002 1 days 03:00:00  03:00:00   27:00:003 0 days 00:00:00  00:00:00    0:00:004 4 days 12:00:00  12:00:00  108:00:005 1 days 03:00:00  03:00:00   27:00:006 0 days 13:30:00  13:30:00   13:30:007 1 days 16:30:00  16:30:00   40:30:008 0 days 00:00:00  00:00:00    0:00:009 1 days 16:30:00  16:30:00   40:30:00


Here's a short and robust version using apply():

df['timediff_string'] = df['timediff'].apply(    lambda x: f'{x.components.hours:02d}:{x.components.minutes:02d}:{x.components.seconds:02d}'              if not pd.isnull(x) else '')

This leverages the components attribute of pandas Timedelta objects and also handles empty values (NaT).

If the timediff column does not contain pandas Timedelta objects, you can convert it:

df['timediff'] = pd.to_timedelta(df['timediff'])


datetime.timedelta already formats the way you'd like. The crux of this issue is that Pandas internally converts to numpy.timedelta.

import pandas as pdfrom datetime import timedeltatime_1 = timedelta(days=3, seconds=3400)time_2 = timedelta(days=0, seconds=3400)print(time_1)print(time_2)times = pd.Series([time_1, time_2])# Times are converted to Numpy timedeltas.print(times)# Convert to string after converting to datetime.timedelta.times = times.apply(    lambda numpy_td: str(timedelta(seconds=numpy_td.total_seconds())))print(times)

So, convert to a datetime.timedelta and then str (to prevent conversion back to numpy.timedelta) before printing.

3 days, 0:56:400:56:4000   3 days 00:56:401   0 days 00:56:40dtype: timedelta64[ns]0    3 days, 0:56:401            0:56:40dtype: object

I came here looking for answers to the same question, so I felt I should add further clarification. : )