从Binance API中提取数据并将其转换为PANDAS Dataframe

时间:2018-01-28 22:15:05

标签: python pandas quantitative-finance

来自Binance的数据是一个python列表,并没有按日期索引

from binance.client import Client
import datetime
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')


client = Client("API_KEY", "API_PRIVATE_KEY")

此功能获取数据并创建2个pd.DataFrames

def get_data(ticker, startday, endday):

    dates = pd.date_range(startday, endday)

    klines = client.get_historical_klines(ticker, Client.KLINE_INTERVAL_1DAY, startday, endday)

此df变量创建按日期

索引的Dataframe
    df = pd.DataFrame(index=dates)

此df_temp变量将binance数据转换为pd.Dataframe

    df_temp = pd.DataFrame(klines)

    del df_temp[0]
    del df_temp[1]
    del df_temp[2]
    del df_temp[3]
    del df_temp[5]
    del df_temp[6]
    del df_temp[7]
    del df_temp[8]
    del df_temp[9]
    del df_temp[10]
    del df_temp[11]

    df_temp = df_temp.rename(columns={4: ticker})

我在这里试图加入两个DataFrames

    df = df.join(df_temp)

    return df

此函数执行脚本

def execute():

    ticker = "BNTETH"

    d = ("1 Dec, 2013", str((datetime.date.today())).split(' ')[0])

    df = get_data(ticker, d[0], d[1])

    print(df)


if __name__ == "__main__":
    execute()

它加入了两个DataFrame,但遗憾的是我得到了这个:

           BNTETH
2013-12-01    NaN
2013-12-02    NaN
2013-12-03    NaN

加入前的数据框:

df.tail()

Empty DataFrame
Columns: []
Index: [2018-01-24 00:00:00, 2018-01-25 00:00:00, 2018-01-26 00:00:00, 
2018-01-27 00:00:00, 2018-01-28 00:00:00]

df_temp

         BNTETH
0    0.01003900
1    0.00924800
2    0.00946400
3    0.00945700
4    0.00945000

2 个答案:

答案 0 :(得分:1)

如果保证dfdf_temp的长度相同,请尝试使用此代替join

df['BNTETH'] = df_temp.values

答案 1 :(得分:1)

这是我的2cents: 首先请求dataSet并对其进行数组:

klines30 = np.array(client.get_historical_klines(symbol, '30m', deltaTime, endTime))

然后此函数使用正确的时间创建一个Dataframe:

def binanceDataFrame(self, klines):
    df = pd.DataFrame(klines.reshape(-1,12),dtype=float, 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'))

    df['Open Time'] = pd.to_datetime(df['Open Time'], unit='ms')


    return df