Alexander Thomsen
Alexander Thomsen

Reputation: 469

KeyError on the 2nd loop on pandas df

so on the first run in the loop everything works fine but on the second loop, it causes a KeyError on the column values on my df. I don't understand why this is happening since in every loop I'm triggering a set of functions.

Part of the code that creates the error

def market_data (crypto, ts_float):

  #request to kraken for pricing data

  r = requests.get('https://futures.kraken.com/api/charts/v1/trade/' + crypto + '/15m?from=' + ts_float)


  #set JSON response to data
  data = r.json()

  #normalize data into dataframe
  df = pd.json_normalize(data, record_path=['candles'])

  #convert unix time back into readable time
  df['time'] = pd.to_datetime(df['time'],unit='ms')

  #set time as index
  df = df.set_index('time')

  #convert into integer for calculations
  df['open'] = df['open'].astype(float).astype(int)
  df['high'] = df['high'].astype(float).astype(int)
  df['low'] = df['low'].astype(float).astype(int)
  df['close'] = df['close'].astype(float).astype(int)
  df['volume'] = df['volume'].astype(float).astype(int)

  return df

crypto_pairs = [
                {"crypto": "pf_ethusd", "size": 0.05},
                {"crypto": "pf_btcusd", "size": 0.0003},
                {"crypto": "pf_avaxusd", "size":3},
                {"crypto": "pf_dotusd", "size":10},
                {"crypto": "pf_ltcusd", "size":1.5}
                ]

 # getting the timstamp to get the data from
ts = (datetime.now() - timedelta(hours = 48)).timestamp()
ts_float = str(int(ts))

for cryptos in enumerate(crypto_pairs):
  data = market_data(cryptos[1]['crypto'], ts_float)

KeyError: time

I have a set of functions in my enumerate loop and the market_data which is the first one generates the mentioned error on the 2nd loop. The errors are always happening when changing the column names such as "time" and "open".

Upvotes: 0

Views: 173

Answers (1)

inquirer
inquirer

Reputation: 4803

I don't have skills in 'request', but this worked for me. Try the following. In the 'deep market_data' function, after receiving the dataframe, set a check, if len(df)<=0, then exit.

Where the dataframe turns out to be empty, the request returns 200, that is, everything is fine. Printed out 'crypto'. An empty dataframe is obtained on 'pf_btcusd'. I tried to swap it and again an empty dataframe turns out to be 'pf_btcusd'. Something is wrong with this symbol.

def market_data (crypto, ts_float):

  #request to kraken for pricing data

  r = requests.get('https://futures.kraken.com/api/charts/v1/trade/' + crypto + '/15m?from=' + ts_float)
  #print(r.status_code)


  #set JSON response to data
  data = r.json()

  #normalize data into dataframe
  df = pd.json_normalize(data, record_path=['candles'])
  if len(df) <=0:
      print(r.status_code)
      print(crypto)
      return

Upvotes: 1

Related Questions