Conversion of Daily pandas dataframe to minute frequency

前端 未结 1 596
长发绾君心
长发绾君心 2021-01-24 00:24

I have a dataframe as defined below (df) with daily frequency and I would like to convert this to minute frequency, starting at 8:30 and ending at 16:00.

import          


        
1条回答
  •  粉色の甜心
    2021-01-24 01:07

    I believe you need reshape by DataFrame.unstack for DatetimeIndex, then set minute frequency by DataFrame.asfreq, filter times by DataFrame.between_time and last use DataFrame.stack for MultiIndex:

    df1 = df.unstack().asfreq('Min', method='ffill').between_time('8:30','16:00').stack()
    print (df1.head(10))
    
                               returns
    date                ticker        
    2016-11-28 08:30:00 aapl       0.2
                        amzn       0.2
                        fb         0.2
                        ge         0.2
                        jpm        0.2
                        msft       0.2
    2016-11-28 08:31:00 aapl       0.2
                        amzn       0.2
                        fb         0.2
                        ge         0.2
    

    0 讨论(0)
提交回复
热议问题