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)
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With