我需要根据以下条件在几个数据间隔上对数据帧进行子集化:
数据帧#1(间隔的实际数据是)
Open High Low Close
DateTime
2011-01-02 00:00:00 1257.75 1257.75 1255.25 1256.00
2011-01-02 02:00:00 1257.50 1257.50 1256.25 1256.75
2011-01-02 04:00:00 1257.75 1257.75 1255.00 1255.25
.
.
.
2011-07-02 00:00:00 1333.00 1335.25 1325.25 1326.75
2011-07-02 02:00:00 1336.50 1338.75 1331.25 1331.50
2011-07-02 04:00:00 1335.75 1337.25 1334.00 1334.25
<class 'pandas.tseries.index.DatetimeIndex'>
[2011-01-02 00:00:00, ..., 2011-07-02 04:00:00]
Length: 2180, Freq: 120T, Timezone: None
DataFrame#2(如果间隔的开始日期时间是,则可以忽略列Num)
Num
DateTime
2011-02-10 00:00:00 0.005117
2011-03-10 00:00:00 -0.010079
2011-04-14 00:00:00 -0.002288
2011-05-12 00:00:00 -0.014116
2011-06-09 00:00:00 -0.000390
<class 'pandas.tseries.index.DatetimeIndex'>
[2010-02-10, ..., 2011-06-09]
Length: 5, Freq: None, Timezone: None
假设你想在'00:00:00'开始你的间隔,你希望它们是3个周期。另请注意df2.index在Freq中的表示方式:无,而df1.index在Freq:120T中。
在我们的假设示例中,正确的输出将如下所示:
2011-02-10 00:00:00 1256.00
2011-02-10 02:00:00 1256.75
2011-02-10 04:00:00 1255.25
2011-03-10 00:00:00 .
2011-03-10 02:00:00 .
2011-03-10 04:00:00 .
2011-04-14 00:00:00 .
2011-04-14 02:00:00 .
2011-04-14 04:00:00 .
2011-05-12 00:00:00 .
2011-05-12 02:00:00 .
2011-05-12 04:00:00 .
2011-06-09 00:00:00 1326.75
2011-06-09 02:00:00 1331.50
2011-06-09 04:00:00 1334.25
请注意,我们使用df2.index(5)中的所有日期作为我们的(3)周期长的间隔的参考,从时间'00:00:00'开始,我们在df1.Close中获取数据
提前致谢。