Binance蜡烛到熊猫数据帧

时间:2018-04-23 12:13:00

标签: python list pandas

from binance.client import Client
import pandas as pd


#Binance Api data
api_key = 'hidden'
api_secret = 'hidden'


#connect Binance
client = Client(api_key, api_secret)


#klines/candlesticks
candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

#create dataframe for candles
candles_dataframe = pd.DataFrame(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', \
             'Can be ignored'])

candle0 = candles[0]
candles_dataframe.append(candle0, ignore_index=True)
print(candles_dataframe)

所以蜡烛是一个返回值的列表,如dataframe列中所述:

[1524425400000, '8918.00000000', '8918.01000000', '8911.07000000', 
'8913.94000000', '9.39563900', 1524425459999, '83771.29790726', 78, 
'6.44918600', '57506.87361929', '0']

我得到了

/Volumes/Data/Dropbox/Dropbox/Coding/btc_forecast/venv/lib/python3.6/site-packages/pandas/core/indexes/api.py:77: RuntimeWarning: '<' not supported between instances of 'str' and 'int', sort order is undefined for incomparable objects`
result = result.union(other)

我的数据框是空的。 我该怎么办?

2 个答案:

答案 0 :(得分:1)

我使用numpy的解决方案

```

from binance.client import Client
import pandas as pd
import numpy as np

client = Client('', '')
def getCandles():
    df = pd.DataFrame(columns= ['Open_time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close_time'])
    candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

    opentime, lopen, lhigh, llow, lclose, lvol, closetime = [], [], [], [], [], [], []

    for candle in candles:
        opentime.append(candle[0])
        lopen.append(candle[1])
        lhigh.append(candle[2])
        llow.append(candle[3])
        lclose.append(candle[4])
        lvol.append(candle[5])
        closetime.append(candle[6])

    df['Open_time'] = opentime
    df['Open'] = np.array(lopen).astype(np.float)
    df['High'] = np.array(lhigh).astype(np.float)
    df['Low'] = np.array(llow).astype(np.float)
    df['Close'] = np.array(lclose).astype(np.float)
    df['Volume'] = np.array(lvol).astype(np.float)
    df['Close_time'] = closetime
    return df

答案 1 :(得分:0)

所以.loc工作: candles_dataframe.loc [0] = candle0