Processing data in real time with pandas

I am receiving live data in the following format:

time  : price
93015 : 10450
93016 : 10450
93020 : 10500

      

as you can see above, time does not return every second. I think he came back every time.

I want to make Open, High, Low, Close data every 5 minutes.

please let me know the way.

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()

      

here is my code above. and when I encode the 4th line then the error message appears

+3


source to share


1 answer


You can use a function resample()

with a method 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  10500

      

+1


source







All Articles