pythonpandasdatetimeindex

How to make a Datetimeindex not be the index in a dataframe


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?


Solution

  • 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.