Johan Nilssons Lifestream

Converting a Pandas datetime index on a DataFrame object to *MultiIndex* with the levels "month" and "year"

Say I have a table of data with monthly datetime indices (the following code gives two years, january through december):

import pandas as pd
import numpy as np
from datetime import datetime
N = 12*2
c = [datetime(1970 + i//12, (i%12)+1, 1) for i in range(N)]
d = pd.DataFrame(np.random.rand(N), index=c)
print(d)

What is the best way to convert the DateTimeIndex into a MultiIndex with the separate levels month and year? Perhaps there is a way to do this with groupby, but I'm not sure.

via Stack Overflow

blog comments powered by Disqus
Get the source for phplifestream at Github