Convert milliseconds column to HH:mm:ss:ms datetime in Pandas

You might want to_timedelta here.

df.time = pd.to_timedelta(df.time, unit='ms')
df.time 

0               NaT
1   00:00:04.067000
2   00:57:24.053000
3   14:01:17.685000
4   16:47:56.311000
5   19:34:35.303000
6   22:21:15.303000
Name: time, dtype: timedelta64[ns]

To get back to square one, use df.total_seconds:

df.time.dt.total_seconds() * 1000

0           NaN
1        4067.0
2     3444053.0
3    50477685.0
4    60476311.0
5    70475303.0
6    80475303.0
Name: time, dtype: float64

You can separate the time portion using

df['time'] = pd.to_datetime(df['time'], unit='ms').dt.time 

the result will be

0                NaN
1    00:00:04.067000
2    00:57:24.053000
3    14:01:17.685000
4    16:47:56.311000
5    19:34:35.303000
6    22:21:15.303000
Name: time, dtype: object

to get the close to the desired output

df['time'] = pd.to_datetime(df['time'],
             unit='ms').dt.strftime('%H:%M:%S:%f').str[:-3] 

this will display the micro seconds using strftime and slicing will convert to milliseconds.

Here is the output of df['time']

0                
1    00:00:04:067
2    00:57:24:053
3    14:01:17:685
4    16:47:56:311
5    19:34:35:303
6    22:21:15:303
Name: time, dtype: object

Tags:

Python

Pandas