Q:

Pandas series sort by month index (time series different) in python

I have a Series object that has:
df = 
    index              value
2014-05-23 07:00:00     0.67
2014-05-23 07:30:00     0.47
2014-05-23 08:00:00     0.42
2014-05-23 08:30:00     0.80
....

2017-07-10 22:00:00     0.42
2017-07-10 22:30:00     0.79
2017-07-10 23:00:00     0.84
2017-07-10 23:30:00     Nan

I would like to calculate the average in a year, and then group by month, so the dataframe would look like this,
df_new = 
  index                    value
   Jan      {0.11, 0.5, 0.3, 0.99, ... ,0.13} <-  time step of each value is 
   Feb      {...............................}     still 30 min, and each 
   Mar      {...............................}     value is average of same 
   Apr      {...............................}     time in the other year.  
   ....
   Dec      {...............................}

I have some dataframe like this but with different time interval (15min, 60min ...), is there any better to calculate it automatically ? For example like function, and it would automatically know the time step from the index. Thanks in advance !

A:

I believe need upsample or downsample first by resample:
#upsample
s = s.resample('15Min').ffill()
#downsample
#s = s.resample('60Min').mean()
#if already 30 minutes values no resample necessary

Then groupby by months by DatetimeIndex.strftime converted to ordered Categorical and DatetimeIndex.time, aggregate mean and last reshape by unstack:
cats = ['Jan', 'Feb', 'Mar', 'Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
months = pd.Categorical(s.index.strftime('%b'), categories=cats, ordered=True)
df = s.groupby([months, s.index.time]).mean().unstack()

