How can I convert the format of time in the dataframe?

I have the colomn with time but it is different from row to row. How can I put in one format?

My colomn looks following:

13:59:47
11:23:36.500000
03:26:05
00:52:10.500000
02:16:24
03:55:06.500000
03:28:51.500000
13:16:08.500000
19:03:45
12:29:03
19:29:26.500000
19:54:07.500000
12:36:14.500000
14:20:00

I need that every value has the same format. For example '%H:%M:%S.S'

The type of the values in colomn is datetime.time

Upvotes: 1

Views: 67

Answers (2)

Bharath_Raja
Bharath_Raja

Reputation: 642

Consider using isoformat so that your microseconds become visible.

from datetime import time

df['column_name'] = df.column_name.apply(lambda x : x.isoformat(timespec='microseconds'))

Upvotes: 1

YOLO
YOLO

Reputation: 21709

You can use strftime to set the format:

df['col'] = pd.to_datetime(df['col']).dt.strftime('%H:%M:%S')

0     13:59:47
1     11:23:36
2     03:26:05
3     00:52:10
4     02:16:24
5     03:55:06
6     03:28:51
7     13:16:08
8     19:03:45
9     12:29:03
10    19:29:26
11    19:54:07
12    12:36:14
13    14:20:00

Upvotes: 1

Related Questions