【发布时间】:2016-06-17 02:22:02
【问题描述】:
我有一个DataFrame,带有一个datetime 索引和一个价格列。我想要 ohlc 数据。 (开、高、低、收)
我想以给定的频率重新采样此数据帧在每一行。
frame.resample('60S', how = 'ohlc') 有效,但现在数据帧的索引间隔 60 秒。我想从前 60 多行的每一行重新采样。 (如果索引相隔 5 秒,则为 12)。这样我就可以为原始数据框中的每一行设置 ohlc 值。
我认为df.resample 无法实现我想做的事情,但.agg 或.map 可能实现?
如何获取每一行行的 ohlc 数据?
n = 10000
prices = np.linspace(100.0, 103.0, n) + np.random.normal(0.0, 0.3, n)
f = pd.DataFrame({'price': prices}, index = pd.date_range(end = datetime.utcnow(), freq = '5S', periods = n))
ohlcized = f.resample('60S', how = 'ohlc') # resampling doesnt work (834 != 10000)
len(ohlcized) # 834
len(f) # 10000
if len(ohlcized) == len(f):
print "question answered"
【问题讨论】:
-
能否提供一些示例数据?
-
完成。如果你需要我澄清更多,我可以。问题是重采样会删除行。我想保留所有数据,并在过去 60 秒的数据中对每一行执行“ohlc”操作。
标签: python numpy pandas dataframe