How do I convert a timestamp column of an object type to an appropriate time type?

I have a dataframe that has a timestamp column as shown 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 the appropriate time type so that the time could be used later. I tried to use pd.to_datetime(df['time'], unit='s', utc=True)but got an error message:

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

So the question is, what is the right way to do this conversion? Thank!

+4
source share
1 answer

That you tried to crash because the unit parameter here expects the input series to be numeric, in this case it is not, and you don't need to pass any arguments 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]

, to_datetime fine

+3

Source: https://habr.com/ru/post/1660704/


All Articles