2017-09-15 71 views
-1

我有以下數據幀:構建在大熊貓定製datetimeindex數據幀

> cal.loc['2007-11-20':'2007-11-30']                                           
         market_open    market_close                                      
2007-11-20 2007-11-20 14:30:00+00:00 2007-11-20 21:00:00+00:00 
2007-11-21 2007-11-21 14:30:00+00:00 2007-11-21 21:00:00+00:00 
2007-11-23 2007-11-23 14:30:00+00:00 2007-11-23 18:00:00+00:00 
2007-11-26 2007-11-26 14:30:00+00:00 2007-11-26 21:00:00+00:00 
2007-11-27 2007-11-27 14:30:00+00:00 2007-11-27 21:00:00+00:00 
2007-11-28 2007-11-28 14:30:00+00:00 2007-11-28 21:00:00+00:00 
2007-11-29 2007-11-29 14:30:00+00:00 2007-11-29 21:00:00+00:00 
2007-11-30 2007-11-30 14:30:00+00:00 2007-11-30 21:00:00+00:00 

我想根據上述數據幀建立與頻率1分鐘和特定範圍的自定義的日期時間索引的每一天。

例如:

2007-11-21 14:30 
2007-11-21 14:31 
2007-11-21 14:32 
2007-11-21 14:33 
2007-11-21 14:34 
... 
2007-11-21 21:00 
2007-11-23 14:30 
2007-11-23 14:31 
... 
2007-11-23 18:00 
2007-11-26 14:30 
... 

感謝

+0

您可以遍歷所有行,並以'1分鐘的頻率將'market_open'的'pd.daterange()'添加到'market_close'? – Bart

回答

0
from functools import reduce 

reduce(
    pd.DatetimeIndex.union, 
    (pd.date_range(o, c, freq='T') for i, o, c in df.itertuples()) 
) 

DatetimeIndex(['2007-11-20 14:30:00', '2007-11-20 14:31:00', 
       '2007-11-20 14:32:00', '2007-11-20 14:33:00', 
       '2007-11-20 14:34:00', '2007-11-20 14:35:00', 
       '2007-11-20 14:36:00', '2007-11-20 14:37:00', 
       '2007-11-20 14:38:00', '2007-11-20 14:39:00', 
       ... 
       '2007-11-30 20:51:00', '2007-11-30 20:52:00', 
       '2007-11-30 20:53:00', '2007-11-30 20:54:00', 
       '2007-11-30 20:55:00', '2007-11-30 20:56:00', 
       '2007-11-30 20:57:00', '2007-11-30 20:58:00', 
       '2007-11-30 20:59:00', '2007-11-30 21:00:00'], 
       dtype='datetime64[ns]', length=2948, freq=None) 
0

您可以通過循環您行併爲每個創建索引,然後在最後

#setup data 
df = pd.DataFrame([["2007-11-20", "2007-11-20 14:30:00", "2007-11-20 21:00:00"], 
        ["2007-11-21", "2007-11-21 14:30:00", "2007-11-21 21:00:00"], 
        ["2007-11-23", "2007-11-23 14:30:00", "2007-11-23 18:00:00"], 
        ["2007-11-26", "2007-11-26 14:30:00", "2007-11-26 21:00:00"], 
        ["2007-11-27", "2007-11-27 14:30:00", "2007-11-27 21:00:00"], 
        ["2007-11-28", "2007-11-28 14:30:00", "2007-11-28 21:00:00"], 
        ["2007-11-29", "2007-11-29 14:30:00", "2007-11-29 21:00:00"], 
        ["2007-11-30", "2007-11-30 14:30:00", "2007-11-30 21:00:00"]], columns=["day", "market_open", "market_close"]) 
df['day'] = pd.to_datetime(df['day']) 
df['market_open'] = pd.to_datetime(df['market_open']) 
df['market_close'] = pd.to_datetime(df['market_close']) 

#code for calculating the index 
idxs = [] 
for i, r in df.iterrows(): 
    idx = pd.DatetimeIndex(freq="1T", start=r['market_open'], end=r['market_close']) 
    idxs.append(idx) 

new_index = pd.Index.append(idxs[0], idxs[1:]) 
指標相結合做到這一點