0

I want to derive a DateTime column from a mixed range of integer column in a panadas dataFrame. The input column is as below. As you see there is a various length of integer numbers in that column. I want to return:

180000 = 18:00:00
60000 = 06:00:00
0 =00:00:00

13 |180000
14 |   0
15 | 60000
16 |100000
17 |   0
18 | 60000

Thanks, Pedram.

1 Answers1

1

Use to_datetime:

df['Time'] = pd.to_datetime(df['value'].replace(0, '0'*6), format='%H%M%S', errors='coerce').dt.time

Result:

   id   value      Time
0  13  180000  18:00:00
1  14       0  00:00:00
2  15   60000  06:00:00
3  16  100000  10:00:00
4  17       0  00:00:00
5  18   60000  06:00:00
Code Different
  • 90,614
  • 16
  • 144
  • 163