[英]Issue on running binance api
我想從 binance 獲取 klines 數據並將其放在 excel 上。 但是有錯誤發生。 這是我的代碼
import pandas as pd
from binance.client import Client
api_key = 'my api key'
api_secret = 'my api key'
client = Client(api_key, api_secret)
klines = client.get_historical_klines('BTCUSDT', Client.KLINE_INTERVAL_1MINUTE, '28 MAY, 2021')
whole_df = pd.DataFrame(klines)
whole_df.columns = ['Open_time','open','high','low','close','volume','Close_time', 'Quote asset volume', 'number of trades', 'Taker buy base asset volume', 'Taker buy quote asset volume', 'Ignore']
whole_df = whole_df.drop_duplicates(subset=['Open_time'], keep=False)
whole_df = whole_df.convert_objects(convert_numeric=True)
whole_df.to_csv('binance_BTCUSDT_data.csv', encoding='utf-8')
exit()
and the error is
File "C:/Users/y/PycharmProjects/untitled/test.py", line 13, in <module>
whole_df = whole_df.convert_objects(convert_numeric=True)
File "C:\Users\y\PycharmProjects\untitled\venv\lib\site-packages\pandas\core\generic.py", line 5465, in __getattr__
return object.__getattribute__(self, name)AttributeError: 'DataFrame' object has no attribute 'convert_objects'
pandas.DataFrame.convert_objects
自 pandas v.0.21.0 以來已棄用。
嘗試改用pandas.to_numeric
。 例如:
設置:
import pandas as pd
data = {
'A': ['1', '2.01', 3],
'B': ['1', '2.01', 3],
}
df = pd.DataFrame(data)
df.info()
安慰:
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 A 3 non-null object
1 B 3 non-null object
dtypes: object(2)
memory usage: 176.0+ bytes
series_numeric = pd.to_numeric(df['A'], downcast='float')
print(series_numeric)
Output:
0 1.00
1 2.01
2 3.00
Name: A, dtype: float32
關於您的幣安問題:我認為沒有必要將 kine 數據轉換為float
或numeric
。 它應該已經在dtype='float'
中。 如果您不確定,請在您的示例中嘗試whole_df.info()
並檢查 dtypes。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.