Reputation: 1675
I have the following Series
and want to extract the day of the datetime
.
import pandas as pd
dates = pd.Series(pd.bdate_range('2017-05-01', '2017-05-05'))
So far, the only solution I can think about is using apply
method:
dates.apply(lambda x: x.day)
However, I noticed that for large pandas dataframe
, the apply
method is not super fast. In a previous code as an example, I had to convert a timedelta
in days and used .astype('timedelta64[D]')
instead of .apply(lambda x: x.days)
, increasing significantly the speed of my code.
I was wondering if anybody had an idea of what I could do to avoid the apply
method for my datetime Series
to extract the day
of my datetime
. Also, if possible, not too specific as I would like to do the same to extract the month
.
Upvotes: 1
Views: 403
Reputation: 375425
You can use the dt
accessor:
In [11]: dates.dt.day
Out[11]:
0 1
1 2
2 3
3 4
4 5
dtype: int64
Upvotes: 4