[英]real time data processing with pandas
我收到的格式如下:
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行時,錯誤信息就會加速
您可以使用ohlc
方法的resample()
函數。
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 10500
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.