Q:

Index or group by attributes of a DateTimeIndex in Pandas

I have a 66-year time series DataFrame of S&P500 monthly change that has a DatetimeIndex.  How can i slice the data into columns of annual data. 
1) something like stacking (and retaining for labels) DateTimeIndex attributes 'month' and 'year'
2) without a loop
--Essentially I want to slice like
c_test[c_test.index.month==1]
c_test[c_test.index.month==2]
c_test[c_test.index.month==3]
c_test[c_test.index.month==4]
....up to 12

--into rows labeled by the 'c_test.index.year' label
Is this possible?  or do I need to ditch the DateTimeIndex? 

A:

Here's a simple way to do it. 
Example using data from https://www.quandl.com/api/v1/datasets/GOOG/NYSE_SPY.csv
1] loading data, adding year and month date
import pandas as pd
df = pd.read_csv('/Users/nicolas/Downloads/GOOG-NYSE_SPY.csv', parse_dates=[0])
df['Year'] = df['Date'].apply(lambda x: x.year)
df['Month'] = df['Date'].apply(lambda x: x.month)
df.head()

output :
        Date    Open    High     Low   Close     Volume  Year  Month
0 2015-02-03  203.00  204.85  202.55  204.84  124212881  2015      2
1 2015-02-02  200.05  202.03  197.86  201.92  163106969  2015      2
2 2015-01-30  200.57  202.17  199.13  199.45  197729724  2015      1
3 2015-01-29  200.38  202.30  198.68  201.99  173585424  2015      1
4 2015-01-28  204.17  204.29  199.91  200.14  168514312  2015      1

2] computing monthly perf, by grouping by Year & Month.  Note the double [ to keep the output as a dataframe on the groupby operation  :
month_perf = df.groupby(['Year', 'Month'])[['Close']].last()
month_perf['month_perf'] = month_perf.pct_change(periods=1)
month_perf = month_perf.reset_index()
month_perf.head()

output :
   Year  Month  Close  month_perf
0  1997      8  92.59         NaN
1  1997      9  93.31    0.007776
2  1997     10  95.62    0.024756
3  1997     11  94.00   -0.016942
4  1997     12  98.09    0.043511

3] Same stuff for the yearly perf, except that we groupby year :
year_perf = df.groupby(['Year'])[['Close']].last()
year_perf['annual_perf'] = year_perf.pct_change(periods=1)
year_perf = year_perf.reset_index()
year_perf.head()

output 
   Year   Close  annual_perf
0  1997   92.59          NaN
1  1998   97.56     0.053678
2  1999  123.03     0.261070
3  2000  145.44     0.182151
4  2001  128.81    -0.114343

4] finally, we merge the two dataframes :
df_result = pd.merge(month_perf, year_perf, left_on='Year', right_on='Year')
print df_result.tail()

output :
     Year  Month  Close_x  month_perf  Close_y  annual_perf
206  2014     10   194.35   -0.031205   182.92     0.252362
207  2014     11   201.77    0.038179   182.92     0.252362
208  2014     12   205.76    0.019775   182.92     0.252362
209  2015      1   205.43   -0.001604   205.43     0.123059
210  2015      2   201.92   -0.017086   205.43     0.123059

