运行binance api的问题

时间:2021-05-29 18:56:36

标签: python api binance

我想从 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'

1 个答案:

答案 0 :(得分:0)

pandas.DataFrame.convert_objectsdeprecated,因为 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

应用pandas.to_numeric

series_numeric = pd.to_numeric(df['A'], downcast='float')
print(series_numeric)

输出:

0    1.00
1    2.01
2    3.00
Name: A, dtype: float32

关于您的币安问题:我认为没有必要将 kine 数据转换为 floatnumeric。它应该已经在 dtype='float' 中。如果您不确定,请在您的示例中尝试 whole_df.info() 并检查 dtype。