George Liu George Liu - 26 days ago 7
Python Question

How to convert a time stamp column of type "object" to proper "time" type?

I have a dataframe in which there's a column of time stamps as below:

0 2016-10-26T00:26:35+00:00
1 2016-10-26T00:26:44+00:00
2 2016-10-26T00:26:37+00:00
3 2016-10-26T00:26:27+00:00
4 2016-10-26T00:26:32+00:00
5 2016-10-26T00:26:37+00:00
6 2016-10-26T00:26:42+00:00
7 2016-10-26T00:26:42+00:00
8 2016-10-26T00:26:45+00:00
9 2016-10-26T00:26:46+00:00
10 2016-10-26T00:26:42+00:00
11 2016-10-26T00:26:46+00:00
12 2016-10-26T00:26:52+00:00
13 2016-10-26T00:26:56+00:00
14 2016-10-26T00:27:00+00:00
15 2016-10-26T00:27:03+00:00
16 2016-10-26T00:27:06+00:00
17 2016-10-26T00:18:28+00:00
18 2016-10-26T00:18:28+00:00
19 2016-10-26T00:18:35+00:00
20 2016-10-26T00:18:31+00:00
21 2016-10-26T00:18:27+00:00
22 2016-10-26T00:18:34+00:00
23 2016-10-26T00:18:43+00:00
24 2016-10-26T00:18:43+00:00
25 2016-10-26T00:18:43+00:00
26 2016-10-26T00:18:50+00:00
27 2016-10-26T00:19:02+00:00
28 2016-10-26T00:19:05+00:00
29 2016-10-26T00:18:39+00:00


I wanted to convert the column to proper "time" type so that the time can be used later. I tried using
pd.to_datetime(df['time'], unit='s', utc=True)
, but got error message:

ValueError: non convertible value 2016-10-26T00:26:35+00:00with the unit 's'


So the question is what's the proper way to do this conversion? Thanks!

Answer

What you tried failed because the unit param here is expecting the the input Series to be numeric which in this case it's not and in fact you don't need to pass any args at all:

In [23]:
pd.to_datetime(df['time'])

Out[23]:
0    2016-10-26 00:26:35
1    2016-10-26 00:26:44
2    2016-10-26 00:26:37
3    2016-10-26 00:26:27
4    2016-10-26 00:26:32
5    2016-10-26 00:26:37
6    2016-10-26 00:26:42
7    2016-10-26 00:26:42
8    2016-10-26 00:26:45
9    2016-10-26 00:26:46
10   2016-10-26 00:26:42
11   2016-10-26 00:26:46
12   2016-10-26 00:26:52
13   2016-10-26 00:26:56
14   2016-10-26 00:27:00
15   2016-10-26 00:27:03
16   2016-10-26 00:27:06
17   2016-10-26 00:18:28
18   2016-10-26 00:18:28
19   2016-10-26 00:18:35
20   2016-10-26 00:18:31
21   2016-10-26 00:18:27
22   2016-10-26 00:18:34
23   2016-10-26 00:18:43
24   2016-10-26 00:18:43
25   2016-10-26 00:18:43
26   2016-10-26 00:18:50
27   2016-10-26 00:19:02
28   2016-10-26 00:19:05
29   2016-10-26 00:18:39
Name: time, dtype: datetime64[ns]

So here to_datetime handles the string fine