I am trying to convert a frame < with multiple indices to a single pandas datetime index.
The index of my original frame frame, i.e. before multi-indexing and unpacking, it looks like this:
In [1]: df1_season.index Out [1]: <class 'pandas.tseries.index.DatetimeIndex'> [2013-05-01 02:00:00, ..., 2014-07-31 23:00:00] Length: 1472, Freq: None, Timezone: None
then I apply multi-indexing and deployment so that I can display annual data on top of each other as follows:
df_sort = df1_season.groupby(lambda x: (x.year, x.month, x.day, x.hour)).agg(lambda s: s[-1]) df_sort.index = pd.MultiIndex.from_tuples(df_sort.index, names=['Y','M','D','H']) unstacked = df_sort.unstack('Y')
My new data frame for the first two days of May is as follows:
In [2]: unstacked Out [2]: temp season Y 2013 2014 2013 2014 MDH 5 1 2 24.2 22.3 Summer Summer 8 24.1 22.3 Summer Summer 14 24.3 23.2 Summer Summer 20 24.6 23.2 Summer Summer 2 2 24.2 22.5 Summer Summer 8 24.8 22.2 Summer Summer 14 24.9 22.4 Summer Summer 20 24.9 22.8 Summer Summer 736 rows × 4 columns
The index for the data frame new , shown above, now looks like this:
In [2]: unstacked.index.values[0:8] Out [2]: array([(5, 1, 2), (5, 1, 8), (5, 1, 14), (5, 1, 20), (5, 2, 2), (5, 2, 8), (5, 2, 14), (5, 2, 20], dtype=object)
which does not create a very pleasant plot regarding xticks (major and minor). If I can convert this multi-index back to a single pandas datetime index using only the data of the month, day and hour, then the main / minor ticks will be automatically constructed as I would like (I think). For instance:
current solution:
xticks = (5, 1, 2), (5, 1, 8) … (5, 2, 20)
Solution Required:
xticks(major) = Day, Month (displayed as MAY 01, MAY 02 etc etc) xticks(minor) = Hour (displayed as 02h 08h … 20h)