user1107049 user1107049 - 1 year ago 181
Python Question

How to convert string timestamp values to datetime64 dtype

The resulting DataFrame below lists the Timestamp values as the strings:

import pandas as pd
df = pd.DataFrame({'Time':['00:00:00:19','02:11:00:07','02:00:40:23']})

What method to use to convert these string values to datetime64 so the sum() and mean() functions could be applied to the column?

Below is the screenshot of the DataFrame as it shown in Notebook:

Answer Source

It's probably not the best way, but it's functional:

durations = (df.Time.str.split(':', expand=True).applymap(int) * [24*60*60, 60*60, 60, 1]).sum(axis=1).apply(pd.Timedelta, unit='s')

Gives you:

0   0 days 00:00:19
1   3 days 08:00:07
2   2 days 00:40:23
dtype: timedelta64[ns]

And durations.sum() will give you Timedelta('5 days 08:40:49')

Okay - slightly easier:

df.Time.str.replace('(\d+):(.*)', r'\1 days \2').apply(pd.Timedelta)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download