Baron Yugovich
Baron Yugovich

Reputation: 4307

Transforming string to datetime in dataframe is slow

I have the following code:

from dateutil import parser
df['time'] = df['time'].apply(lambda x: parser.parse(x))

I have few hundred thousand rows, and that line takes tens of seconds. Is there any way to optimize it?

Upvotes: 0

Views: 47

Answers (1)

BENY
BENY

Reputation: 323266

Using pd.to_datetime

%timeit df['time'].apply(lambda x: parser.parse(x))
1 loop, best of 3: 812 ms per loop
%timeit pd.to_datetime(df.time)
100 loops, best of 3: 4.25 ms per loop


len(df)
Out[290]: 20000

Upvotes: 1

Related Questions