Question: I have a pandas DataFrame with a datetimeindex. I would like to create a `label` column that is made of year and month of my data.

The way that I have found to do is by making a copy of the column and operating on it (I have to do this because a `DateTimeIndex` has no `apply`method). I am sure there must be a way to operate on the index directly though but I could not find it:

```
import pandas  as pd
import numpy as np

df = pd.DataFrame(index=pd.date_range(start="2012-01-01", end="2013-01-01", freq='D'), data=range(367))

monthly = df.resample("M")
monthly["label"] = monthly.index
monthly["label"] = monthly["label"].apply(lambda x: x.strftime("%Y-%m"))

```

Where `monthly.head()` gives me:

```
                0    label
2012-01-31   15.0  2012-01
2012-02-29   45.0  2012-02
2012-03-31   75.0  2012-03
2012-04-30  105.5  2012-04
2012-05-31  136.0  2012-05

```

Which is exactly what I want, I would just like to do it without the second last line in the source that I use as a workaround.

Answer: 
There might be a slicker way to get the strftime result directly, but in general when you want to treat an Index as if it were a column without actually making it one, you can call [`.to_series()`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.to_series.html) instead:

```
>>> monthly.index.to_series().head()
2012-01-31   2012-01-31
2012-02-29   2012-02-29
2012-03-31   2012-03-31
2012-04-30   2012-04-30
2012-05-31   2012-05-31
Freq: M, dtype: datetime64[ns]
>>> monthly.index.to_series().apply(lambda x: x.strftime("%Y-%m")).head()
2012-01-31    2012-01
2012-02-29    2012-02
2012-03-31    2012-03
2012-04-30    2012-04
2012-05-31    2012-05
Freq: M, dtype: object

```
Another way could be using `np.array` to cast the date to `<M8[M]` monthly.

```
df.index.values.astype('<M8[M]').astype(str)

array(['2012-01', '2012-01', '2012-01', ..., '2012-12', '2012-12', '2013-01'], 
      dtype='<U25')

```