看来您需要通过MultiIndex.from_arrays
添加:times
to_timedelta
time1 = '09:30:00'
time2 = '15:30:00'
df.index = pd.MultiIndex.from_arrays([df.index + pd.to_timedelta(time1),
df.index + pd.to_timedelta(time2)],
names=['date1','date2'])
print (df)
Open_fut Close_fut
date1 date2
2017-05-12 09:30:00 2017-05-12 15:30:00 20873.0 20850.0
2017-05-11 09:30:00 2017-05-11 15:30:00 20887.0 20869.0
2017-05-10 09:30:00 2017-05-10 15:30:00 20891.0 20888.0
2017-05-09 09:30:00 2017-05-09 15:30:00 20943.0 20886.0
2017-05-08 09:30:00 2017-05-08 15:30:00 21001.0 20943.0
对于您的输出是类似的解决方案,仅用于lreshape
重塑 + set_index
+ sort_index
:
time1 = '09:30:00'
time2 = '15:30:00'
df['date1'] = df.index + pd.to_timedelta(time1)
df['date2'] = df.index + pd.to_timedelta(time2)
df = pd.lreshape(df, {'date':['date1', 'date2'], 'fut':['Open_fut', 'Close_fut']})
df = df.set_index('date').sort_index()
print (df)
fut
date
2017-05-08 09:30:00 21001.0
2017-05-08 15:30:00 20943.0
2017-05-09 09:30:00 20943.0
2017-05-09 15:30:00 20886.0
2017-05-10 09:30:00 20891.0
2017-05-10 15:30:00 20888.0
2017-05-11 09:30:00 20887.0
2017-05-11 15:30:00 20869.0
2017-05-12 09:30:00 20873.0
2017-05-12 15:30:00 20850.0
编辑:
lreshape
现在没有记录,但将来可能会被删除(也有 pd.wide_to_long)。
可能的解决方案是将所有 3 个功能合并为一个 - 也许melt
,但现在它没有实现。也许在一些新版本的熊猫中。然后我的答案将被更新。