【问题标题】:Issue on running binance api运行binance api的问题
【发布时间】:2021-05-29 18:56:36
【问题描述】:

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

【问题讨论】:

  • 您看到的错误有什么不清楚的地方?

标签: python api binance


【解决方案1】:

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() 并检查 dtypes。

【讨论】:

    猜你喜欢
    • 2022-07-26
    • 2021-05-06
    • 2022-07-25
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2016-09-16
    相关资源
    最近更新 更多