我正在接收以下格式的实时数据:
time : price
93015 : 10450
93016 : 10450
93020 : 10500正如你在上面看到的,时间不是每一秒都会回来的。我想它每一次都会回来。
我想使开放,高,低,关闭价格数据在每5分钟。
请告诉我怎么走。
ctime = com_obj.GetHeaderValue(18) #get realtime time data, type=integer
time_stamp = ctime
cur_price = com_obj.GetHeaderValue(13) #get realtime price data type=integer
df = pd.DataFrame(dict(price=cur_price, time_stamp=time_stamp)).set_index('time_stamp').sort_index()这是我上面的代码。当我编码第4行时,会弹出错误消息。
发布于 2015-06-30 14:50:50
您可以将resample()函数与ohlc方法结合使用。
import pandas as pd
import numpy as np
#from io import StringIO # if you use python 3.x
from StringIO import StringIO # if you use python 2.x
raw_data_string_buffer = 'time : price\n93015 : 10450\n93016 : 10450\n93020 : 10500\n'
print(raw_data_string_buffer)
time : price
93015 : 10450
93016 : 10450
93020 : 10500
# replace the stringio part with your file path
data = pd.read_csv(StringIO(raw_data_string_buffer), header=0, sep=':', names=['time', 'price'])
data['time'] = pd.to_datetime(data['time'], format='%H%M%S')
data = data.set_index(['time'])
data.resample('5min', how='ohlc')
Out[177]:
price
open high low close
time
1900-01-01 09:30:00 10450 10500 10450 10500https://stackoverflow.com/questions/31141292
复制相似问题