Key error while converting get_bars to dataframe

I have the market data subscription. I am having this issue from this morning. Will appreciate if anyone could help.
data1 = api_data.get_bars(symbol, TimeFrame.Hour, start, end, adjustment=‘raw’).df
gives me error :

KeyError Traceback (most recent call last)
in
10 api_data = tradeapi.REST(API_KEY, API_SECRET, API_BASE_URL, ‘v2’)
11
—> 12 data1 = api_data.get_bars(symbol, TimeFrame.Hour, start, end, adjustment=‘raw’).df
13
14 print(data1.head())

c:\users\mushf\documents\alpaca-trading\env\lib\site-packages\alpaca_trade_api\entity_v2.py in df(self)
59 )
60
—> 61 df.columns = [self.mapping[c] for c in df.columns]
62 if not df.empty:
63 df.set_index(‘timestamp’, inplace=True)

c:\users\mushf\documents\alpaca-trading\env\lib\site-packages\alpaca_trade_api\entity_v2.py in (.0)
59 )
60
—> 61 df.columns = [self.mapping[c] for c in df.columns]
62 if not df.empty:
63 df.set_index(‘timestamp’, inplace=True)

KeyError: ‘n’

I am getting the same key error

Try updating entity_v2.py to

bar_mapping_v2 = {
“S”: “symbol”,
“o”: “open”,
“h”: “high”,
“l”: “low”,
“c”: “close”,
“v”: “volume”,
“t”: “timestamp”,
“n”: “n”,
“vw”: “vw”,
}
web api has added two extra fields.

Yep, that fixed it. Thanks for the heads up.

Pandas KeyError occurs when we try to access some column/row label in our DataFrame that doesn’t exist. Usually, this error occurs when you misspell a column/row name or include an unwanted space before or after the column/row name… Before doing anything with the data frame, use print(df.columns) to see pandas column exist or not.

print(df.columns)

I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.