How to make a Datetimeindex not be the index in a dataframe
Question:
I’d like to have access to the special methods provided by the Datetimeindex class such as month, day, etc. However I can’t seem to make a series in a dataframe be a Datetimeindex without making it the dataframe’s index. Take the following example:
dates
Out[119]:
Dates
0 1/1/2012
1 1/2/2012
2 1/3/2012
3 1/4/2012
4 1/5/2012
5 1/6/2012
6 1/7/2012
7 1/8/2012
8 1/9/2012
9 1/10/2012
10 12/31/2012
date_series = pd.DatetimeIndex(dates.Dates)
date_series.month
Out[115]: array([ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12])
dates.Dates = pd.DatetimeIndex(dates.Dates)
dates.Dates.month
AttributeError: 'Series' object has no attribute 'month'
I also tried converting the series to timestamps using pd.to_datetime but it still doesn’t work.
I know I can work around this, but it seems like this functionality should exist?
Answers:
Update: in 0.15 you will have access to a dt attribute for datetimelike methods:
dates.Dates.dt.month
Old post (do use Wes’ solution and not this):
Here’s one (slow!) workaround to do it using apply
, not ideal but it works:
In [11]: from pandas.lib import Timestamp
In [12]: df.Dates.apply(lambda x: Timestamp(x).month)
Out[12]:
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 12
Name: Dates
It seems like a bug (that you can’t do apply(lambda x: x.month)
), perhaps worth adding as an issue on github. As Wes would say: “welcome to hell”.
For now I would suggest doing pd.DatetimeIndex(dates.Dates).month
. I’ve been debating whether to add a bunch of data type-specific attributes to Series that will only work for timestamps, but haven’t done it yet.
I’d like to have access to the special methods provided by the Datetimeindex class such as month, day, etc. However I can’t seem to make a series in a dataframe be a Datetimeindex without making it the dataframe’s index. Take the following example:
dates
Out[119]:
Dates
0 1/1/2012
1 1/2/2012
2 1/3/2012
3 1/4/2012
4 1/5/2012
5 1/6/2012
6 1/7/2012
7 1/8/2012
8 1/9/2012
9 1/10/2012
10 12/31/2012
date_series = pd.DatetimeIndex(dates.Dates)
date_series.month
Out[115]: array([ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 12])
dates.Dates = pd.DatetimeIndex(dates.Dates)
dates.Dates.month
AttributeError: 'Series' object has no attribute 'month'
I also tried converting the series to timestamps using pd.to_datetime but it still doesn’t work.
I know I can work around this, but it seems like this functionality should exist?
Update: in 0.15 you will have access to a dt attribute for datetimelike methods:
dates.Dates.dt.month
Old post (do use Wes’ solution and not this):
Here’s one (slow!) workaround to do it using apply
, not ideal but it works:
In [11]: from pandas.lib import Timestamp
In [12]: df.Dates.apply(lambda x: Timestamp(x).month)
Out[12]:
0 1
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 12
Name: Dates
It seems like a bug (that you can’t do apply(lambda x: x.month)
), perhaps worth adding as an issue on github. As Wes would say: “welcome to hell”.
For now I would suggest doing pd.DatetimeIndex(dates.Dates).month
. I’ve been debating whether to add a bunch of data type-specific attributes to Series that will only work for timestamps, but haven’t done it yet.