Reputation: 31
The data from Binance comes as a python list and does not come indexed by date
from binance.client import Client
import datetime
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
client = Client("API_KEY", "API_PRIVATE_KEY")
This Funtion gets the data and creates 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)
This df variable creates a Dataframe indexed by date
df = pd.DataFrame(index=dates)
This df_temp variable turns binance data into a 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})
Here I'm trying to join both DataFrames
df = df.join(df_temp)
return df
This function executes the script
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()
It joins both DataFrames but unfortunately I get this:
BNTETH
2013-12-01 NaN
2013-12-02 NaN
2013-12-03 NaN
Dataframes before the join:
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
Upvotes: 1
Views: 10751
Reputation: 31
this is my 2cents: first request dataSet and array it:
klines30 = np.array(client.get_historical_klines(symbol, '30m', deltaTime, endTime))
then this function creates a Dataframe with the correct time:
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
Upvotes: 3
Reputation: 11105
If df
and df_temp
are guaranteed to be the same length, try this instead of the join
:
df['BNTETH'] = df_temp.values
Upvotes: 1