Python error

While working in module for "Python for Trading" for one of the codes i am getting the below error. I am new to programming. Please help



Below is the code:-

infy_close = infy [['Date','Close price']]

infy_close.set_index ('Date', inplace=True)

infy_close




Below is the error:-

 

KeyError                                  Traceback (most recent call last)
<ipython-input-7-03ddb99b4dad> in <module>
----> 1 infy_close = infy [['Date','Close price']]
      2 infy_close.set_index ('Date', inplace=True)
      3 infy_close
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2804             if is_iterator(key):
   2805                 key = list(key)
-> 2806             indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
   2807 
   2808         # take() does not accept boolean indexers
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1550 
   1551         self._validate_read_indexer(
-> 1552             keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
   1553         )
   1554         return keyarr, indexer
~\anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
1643             if not (self.name == "loc" and not raise_missing):
   1644                 not_found = list(set(key) - set(ax))
-> 1645                 raise KeyError(f"{not_found} not in index")
   1646 
   1647             # we skip the warning on Categorical/Interval

 

KeyError: "['Date'] not in index"

As it says in the error, its a KeyError. You can check it at the beginning (says "KeyError") and the end of the error log. At the end, it shows you which Key does not exits:

 

KeyError: "['Date'] not in index"




It says that "Date" is not in your index. Maybe its "date" or "DateTime"?

Hi Mario,



Thanks for the reply. I checked there was spacing in key items in excel spreadhseet.



Able to resolve the errors now.



Thanks