sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
6.06k stars 2.21k forks source link

ValueError: Length mismatch: Expected axis has 0 elements, new values have 5 elements #1122

Open VincentDuf opened 2 years ago

VincentDuf commented 2 years ago

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

Here is the main function I use to get these data:

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"))

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

Despite some research on stackoverflow or other site I still don't get the error... if anyone can help me.

Thanks :)

[EDIT]: It seems that it happens for certain pairs, regularly 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... As a result I have only 4 over 300 pairs that are properly saved which is very few...

VincentDuf commented 2 years ago

No one ? @sammchardy Even when I pre-create a DataFrame with 5 empty columns (or with one row) I still get the error... is it coming from the binance side or is it pandas ?

Thanks

katkoe commented 2 years ago

Not sure if this is too late, but I'm pretty sure this error is returned for delisted pairs. The Binance api can return an empty dict for these pairs, so the dataframe is empty in these instances and therefore has 0 columns. When you try to assign 5 column names (in new_df.columns = ['Time','open','high','low','close']) to a 0 column dataframe Pandas throws out this error.

Simmelariii commented 2 years ago

katkoe This is not true, i get the same error after looping through 5-100 times using only btcusdt

Skärmavbild 2022-07-10 kl  00 52 13

If you know another soulution i would love to hear it, thanks!

Dhruv-16 commented 2 years ago

Has anyone solved this yet?

asset,interval,lookback are global variable so no issue with that just that i think there is no stream of datasets to pd from binance api even though the api is up and running i tested the api by calling (info = client.get_account())

Screenshot (10)