Convert pandas multi-index to pandas timestamp
Converting data back and forth in pandas gets messy very fast, as you seem to have experienced.My recommendation in general concerning pandas and indexing, is to never just set the index, but to copy it first. Make sure you have a column which contains the index, since pandas does not allow all operations on the index, and intense setting and resetting of the index can cause columns to dissapear.
TLDR;Don't convert the index back. Keep a copy.
import pandas as pdimport matplotlib.pyplot as pltfrom numpy.random import randnts = pd.Series(randn(1000), index=pd.date_range('1/1/2000', periods=1000))ts = ts.cumsum()plt.figure()for year in set(ts.index.year): tmp = ts[str(year)].values plt.plot(tmp, label = year)plt.legend()plt.show()
I think this is a better way to accomplish your goal than re-indexing. What do you think?