# Key error while converting get\_bars to dataframe

**URL:** <https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092>\
**Category:** Alpaca Market Data\
**Created:** [June 30, 2021, 2:26pm UTC](https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092 "2021-06-30T14:26:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![poseidon](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@poseidon](https://forum.alpaca.markets/u/poseidon)\
**Post date:** [June 30, 2021, 2:26pm UTC](https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092/1 "2021-06-30T14:26:01Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![onesNzeros](https://avatars.discourse-cdn.com/v4/letter/o/ac91a4/32.png) [@onesNzeros](https://forum.alpaca.markets/u/onesNzeros)\
**Post date:** [June 30, 2021, 2:31pm UTC](https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092/2 "2021-06-30T14:31:51Z")

</div>

I am getting the same key error

---

<div class="post-metadata">

**Author:** ![poseidon](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@poseidon](https://forum.alpaca.markets/u/poseidon)\
**Post date:** [June 30, 2021, 2:46pm UTC](https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092/3 "2021-06-30T14:46:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![onesNzeros](https://avatars.discourse-cdn.com/v4/letter/o/ac91a4/32.png) [@onesNzeros](https://forum.alpaca.markets/u/onesNzeros)\
**Post date:** [June 30, 2021, 3:13pm UTC](https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092/4 "2021-06-30T15:13:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![macronbell](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@macronbell](https://forum.alpaca.markets/u/macronbell)\
**Post date:** [January 18, 2023, 6:35am UTC](https://forum.alpaca.markets/t/key-error-while-converting-get-bars-to-dataframe/6092/5 "2023-01-18T06:35:14Z")

</div>

> [@poseidon](#):
>
> KeyError: ‘n’

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](http://net-informations.com/ds/pd/exists.htm) 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.
