Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up column conversions

Tags:

python

pandas

Say I have a large dataframe and that I want to apply one operation to every element in a column.

Is there a faster way of doing it than the following:

get_weekday = lambda x: time.strptime(str(x), '%d%m%Y').tm_wday
df['date'] = df['date'].apply(get_weekday)

?

like image 476
Amelio Vazquez-Reina Avatar asked Mar 11 '26 09:03

Amelio Vazquez-Reina


1 Answers

In current master/0.15.0

df['date'].dt.weekday

In prior versions

pd.DatetimeIndex(df['date']).weekday

Here's a timing example

In [16]: s = Series(date_range('20130101',freq='s',periods=100000))

In [17]: %timeit s.dt.weekday
10 loops, best of 3: 50.8 ms per loop

In [18]: s2 = s.apply(str)

In [19]: %timeit s.apply(lambda x: time.strptime(str(x), "%Y-%m-%d %H:%M:%S").tm_wday)
1 loops, best of 3: 2.65 s per loop
like image 155
Jeff Avatar answered Mar 12 '26 23:03

Jeff