我在开放范围/第一小时(美国东部时间上午9:30-10:30)有基于分钟的OHLCV数据。我想重新采样这些数据,这样我可以获得一个60分钟的值,然后计算范围。
当我在数据上调用dataframe.resample()函数时,我得到两行,初始行在上午9:00开始。我期待只有一行从上午9:30开始。
注意:初始数据从9:30开始。
编辑:添加代码:
# Extract data for regular trading hours (rth) from the 24 hour data set
rth = data.between_time(start_time = '09:30:00', end_time = '16:15:00', include_end = False)
# Extract data for extended trading hours (eth) from the 24 hour data set
eth = data.between_time(start_time = '16:30:00', end_time = '09:30:00', include_end = False)
# Extract data for initial balance (rth) from the 24 hour data set
initial_balance = data.between_time(start_time = '09:30:00', end_time = '10:30:00', include_end = False)
试图按个别日期分开开场范围并获得初始余额
conversion = {'Open' : 'first', 'High' : 'max', 'Low' : 'min', 'Close' : 'last', 'Volume' : 'sum'}
sample = data.between_time(start_time = '09:30:00', end_time = '10:30:00', include_end = False)
sample = sample.ix['2007-05-07']
sample.tail()
sample.resample('60Min', how = conversion)
默认情况下,重新采样从小时的开始处开始。我希望它从数据开始的地方开始。
答案 0 :(得分:23)
您可以使用resample
的base
参数:
sample.resample('60Min', how=conversion, base=30)
base
:int
,默认0
对于均匀细分1天的频率,聚合间隔的“原点” 例如,对于“5分钟”频率,基数可以在0到4之间。默认值为0
答案 1 :(得分:0)
value 是您要聚合的列,按秒重新采样数据帧日期并按平均值聚合,然后删除 nan 行。
data=[('2014-02-24 16:16:47.204000', 1.391424)
,('2014-02-24 16:18:48.296000', 1.048143)
,('2014-02-24 16:19:52.346000', -0.823974)
,('2014-02-24 16:22:13.665000', -0.689560)
,('2014-02-24 16:24:13.760000', -0.323252)
,('2014-02-24 16:26:15.155000', -1.095331)
,('2014-02-24 16:29:58.235000', -0.185681)]
df=pd.DataFrame(data,columns=['Date','Value'])
df['Date']=pd.to_datetime(df['Date'])
minutes=df.resample('1Min',on='Date').mean().dropna()
print(minutes)
输出:
Value
Date
2014-02-24 16:16:00 1.391424
2014-02-24 16:18:00 1.048143
2014-02-24 16:19:00 -0.823974
2014-02-24 16:22:00 -0.689560
2014-02-24 16:24:00 -0.323252
2014-02-24 16:26:00 -1.095331
2014-02-24 16:29:00 -0.185681