如何将“对象”类型的时间戳列转换为正确的“时间”类型?

时间:2016-11-14 16:30:01

标签: python pandas dataframe

我有一个数据框,其中有一列时间戳如下:

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

我想将列转换为正确的“时间”类型,以便以后可以使用时间。我尝试使用pd.to_datetime(df['time'], unit='s', utc=True),但收到了错误消息:

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

所以问题是这种转换的正确方法是什么?谢谢!

1 个答案:

答案 0 :(得分:4)

您尝试失败的原因是因为此处的单位参数期望输入系列为数字,在这种情况下它不是数字,实际上您根本不需要传递任何参数:

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]

所以这里to_datetime处理字符串