繁体   English   中英

运行币安 api 的问题

[英]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

应用pandas.to_numeric

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 数据转换为floatnumeric 它应该已经在dtype='float'中。 如果您不确定,请在您的示例中尝试whole_df.info()并检查 dtypes。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM