Jupyter Notebook AttributeError: SQL Database in Python
Jupyter Notebook AttributeError: SQL Database in Python
我的问题是如何解决这个 AttributeError ,代码如下:
import pandas as pd
import sqlalchemy
from binance import Client
from binance import BinanceSocketManager
client = Client('api', 'secret_api')
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket('BTCUSDT')
while True:
await socket.__aenter__()
msg = await socket.recv()
frame = createframe(msg)
frame.to_sql('BTCUSDT', engine, if_exists='append', index=False)
print(frame)
但它向我提出了以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11340/3652326046.py in <module>
3 msg = await socket.recv()
4 frame = createframe(msg)
----> 5 frame.to_sql('BTCUSDT', engine, if_exists='append', index=False)
6 print(frame)
AttributeError: 'NoneType' object has no attribute 'to_sql'
这里是“createframe”函数:
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:,['s','E','p']]
df.columns = ['symbol','Time','Price']
df.Price = df.Price.astype(float)
df.Time = pd.to_datetime(df.Time, unit='ms')
您的 createframe
方法似乎没有 return 任何东西。我建议您将其更改为:
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:,['s','E','p']]
df.columns = ['symbol','Time','Price']
df.Price = df.Price.astype(float)
df.Time = pd.to_datetime(df.Time, unit='ms')
return df # Here is the change where it returns your DataFrame to where it is called from
我希望这能奏效。真的不能 post 因为我们缺乏你的问题的更多背景
我的问题是如何解决这个 AttributeError ,代码如下:
import pandas as pd
import sqlalchemy
from binance import Client
from binance import BinanceSocketManager
client = Client('api', 'secret_api')
bsm = BinanceSocketManager(client)
socket = bsm.trade_socket('BTCUSDT')
while True:
await socket.__aenter__()
msg = await socket.recv()
frame = createframe(msg)
frame.to_sql('BTCUSDT', engine, if_exists='append', index=False)
print(frame)
但它向我提出了以下错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11340/3652326046.py in <module>
3 msg = await socket.recv()
4 frame = createframe(msg)
----> 5 frame.to_sql('BTCUSDT', engine, if_exists='append', index=False)
6 print(frame)
AttributeError: 'NoneType' object has no attribute 'to_sql'
这里是“createframe”函数:
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:,['s','E','p']]
df.columns = ['symbol','Time','Price']
df.Price = df.Price.astype(float)
df.Time = pd.to_datetime(df.Time, unit='ms')
您的 createframe
方法似乎没有 return 任何东西。我建议您将其更改为:
def createframe(msg):
df = pd.DataFrame([msg])
df = df.loc[:,['s','E','p']]
df.columns = ['symbol','Time','Price']
df.Price = df.Price.astype(float)
df.Time = pd.to_datetime(df.Time, unit='ms')
return df # Here is the change where it returns your DataFrame to where it is called from
我希望这能奏效。真的不能 post 因为我们缺乏你的问题的更多背景