1 year ago

#73913

test-img

Vince Dfr

Binance python API - ValueError: Length mismatch: Expected axis has 0 elements, new values have 5 elements

I'm trying to get the daily data from all BUSD and USDT pairs from Binance over the past 2 years.

I have created 2 lists of BUSD and USDT pairs like this: busd_list = ["BTCBUSD","ETHBUSD","BNBBUSD"...]

Then for each element of this list I want to execute the function below:

def klines_2_dataframe(ticker):
    new_df = pd.DataFrame(client.get_historical_klines(ticker,"1d","1 Jan, 2020"))
    new_df = new_df.iloc[:,:5]
    new_df.columns = ['Time','open','high','low','close']
    new_df[["open","high","low","close"]] = new_df[["open","high","low","close"]].astype(float)
    new_df.Time = pd.to_datetime(new_df.Time,unit="ms")
    return new_df

And how I want to save those dataframe inside a SQLite database:

db = sqlite3.connect(os.path.join(os.getcwd(),"busd_pairs.sqlite"))
for tickers in busd_symbols:
   df = klines_2_dataframe(tickers)
   df.to_sql(tickers,db,index=False,index_label=df.Time,if_exists='replace')

Weirdly when I execute this line alone:

new_df = pd.DataFrame(client.get_historical_klines(ticker,"1d","1 Jan, 2020"))

whatever the ticker value, there is no error.

But when I run it inside a loop I get this error over and over: ValueError: Length mismatch: Expected axis has 0 elements, new values have 5 elements

It seems that it happens for lots of pairs, but I still don't get why. It also seems to be an issue about the number of columns of my dataframe but I don't get why some are an issue and not other...

If anyone can help me.

Thanks :)

python-3.x

pandas

sqlite

binance

binance-api-client

0 Answers

Your Answer

Accepted video resources