How to efficiently convert a dataframe column of type string to datetime in Python?

I have a column with identifiers and the time is encoded internally. For instance:

0    020160910223200_T1
1    020160910223200_T1
2    020160910223203_T1
3    020160910223203_T1
4    020160910223206_T1
5    020160910223206_T1
6    020160910223209_T1
7    020160910223209_T1
8    020160910223213_T1
9    020160910223213_T1

If we delete the first and last three characters, we get for the first line: 20160910223200, which should be converted to 2016-09-10 22:32:00.

My solution was to write a function that truncates identifiers and converts them to datetime. Then I applied this function to the df column.

from datetime import datetime
def MeasureIDtoTime(MeasureID):
    MeasureID = str(MeasureID)
    MeasureID = MeasureID[1:14]
    Time = datetime.strptime(MeasureID, '%Y%m%d%H%M%S')
    return Time
df['Time'] = df['MeasureID'].apply(MeasureIDtoTime)

This works correctly, however for my case it is slow. I have to deal with over 20 million lines and I need a faster solution. Any idea for a better solution?

Update

According to @MaxU there is a better solution:

pd.to_datetime(df.ID.str[1:-3], format = '%Y%m%d%H%M%S')

32 7,2 . R lubridate::ymd_hms() 2 . , Python.

+4
1

UPDATE: ...

DF: 50.000 x 1

In [220]: df.head()
Out[220]:
                   ID
0  020160910223200_T1
1  020160910223200_T1
2  020160910223203_T1
3  020160910223203_T1
4  020160910223206_T1

In [221]: df.shape
Out[221]: (50000, 1)

In [222]: len(df)
Out[222]: 50000

Timing:

In [223]: %timeit df['ID'].apply(MeasureIDtoTime)
1 loop, best of 3: 929 ms per loop

In [224]: %timeit pd.to_datetime(df.ID.str[1:-3])
1 loop, best of 3: 5.68 s per loop

In [225]: %timeit pd.to_datetime(df.ID.str[1:-3], format='%Y%m%d%H%M%S')
1 loop, best of 3: 267 ms per loop    ### WINNER !

:, , 21 .

: , datetime

OLD answer:

In [81]: pd.to_datetime(df.ID.str[1:-3])
Out[81]:
0   2016-09-10 22:32:00
1   2016-09-10 22:32:00
2   2016-09-10 22:32:03
3   2016-09-10 22:32:03
4   2016-09-10 22:32:06
5   2016-09-10 22:32:06
6   2016-09-10 22:32:09
7   2016-09-10 22:32:09
8   2016-09-10 22:32:13
9   2016-09-10 22:32:13
Name: ID, dtype: datetime64[ns]

df:

In [82]: df
Out[82]:
                   ID
0  020160910223200_T1
1  020160910223200_T1
2  020160910223203_T1
3  020160910223203_T1
4  020160910223206_T1
5  020160910223206_T1
6  020160910223209_T1
7  020160910223209_T1
8  020160910223213_T1
9  020160910223213_T1
+7

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


All Articles