Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将Websocket数据保存到Pandas_Python_Pandas_Websocket - Fatal编程技术网

Python 将Websocket数据保存到Pandas

Python 将Websocket数据保存到Pandas,python,pandas,websocket,Python,Pandas,Websocket,我正在尝试使用websocket数据并将其保存到pandas数据框中,以供其他函数使用。但是,我对它们非常不熟悉,最初出现错误是因为我试图直接将df参数传递给on_message()。link建议使用partial添加参数,但我仍然从回调中得到一个错误:赋值前引用了局部变量“df”错误 我意识到有更好的方法来处理数据,而不是数据帧,但我只想先让它工作起来。谢谢 import websocket import pandas as pd import json from functools impo

我正在尝试使用websocket数据并将其保存到pandas数据框中,以供其他函数使用。但是,我对它们非常不熟悉,最初出现错误是因为我试图直接将df参数传递给on_message()。link建议使用
partial
添加参数,但我仍然从回调中得到一个
错误:赋值前引用了局部变量“df”
错误

我意识到有更好的方法来处理数据,而不是数据帧,但我只想先让它工作起来。谢谢

import websocket
import pandas as pd
import json
from functools import partial

# create empty df
df = pd.DataFrame(columns=['foreignNotional','grossValue','homeNotional','price','side','size','symbol','tickDirection',
                          'timestamp','trdMatchID'])

def on_message(ws, message):
    msg = json.loads(message)
    print(msg)
    df = df
    df = df.append(msg)

def on_error(ws, error):
    print(error)

def on_close(ws):
    print("### closed ###")

def on_open(ws):
    return

func = partial(on_message, df)
ws.on_message = func

if __name__ == "__main__":
    #websocket.enableTrace(True)
    ws = websocket.WebSocketApp("wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD",
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever()

由于您必须修改外部对象,并且
pandas.DataFrame.append
不允许就地修改,因此您必须使所有函数和
df
作为某个类的成员可用,以便能够编写
self.df=self.df.append(msg)
,或使用
global

import json

import pandas as pd
import websocket

df = pd.DataFrame(columns=['foreignNotional', 'grossValue', 'homeNotional', 'price', 'side',
                           'size', 'symbol', 'tickDirection', 'timestamp', 'trdMatchID'])


def on_message(ws, message):
    msg = json.loads(message)
    print(msg)
    global df
    # `ignore_index=True` has to be provided, otherwise you'll get
    # "Can only append a Series if ignore_index=True or if the Series has a name" errors
    df = df.append(msg, ignore_index=True)


def on_error(ws, error):
    print(error)


def on_close(ws):
    print("### closed ###")


def on_open(ws):
    return


if __name__ == "__main__":
    ws = websocket.WebSocketApp("wss://www.bitmex.com/realtime?subscribe=trade:XBTUSD",
                                on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close)
    ws.run_forever()