运行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_objectsdeprecated,因为 pandas v.0.21.0。

尝试改用 pandas.to_numeric。例如:


设置:

import pandas as pd

data = {
    'A': ['1','2.01',3],'B': ['1',}
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。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...