Python Forum

Full Version: Trying to get a "simple" code to work, re: 3.5 vs 3.8 vs Not using Linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I have this code as an example test to check out moving averages from a .csv file, using Jupyter notebook (although I uninstalled version 3.8 about 10 minutes ago, but I don't think the errors are any different if it is using my 3.5 as installed or it's own web version of 3.8):

%matplotlib inline

import pandas as pd

data = pd.read_csv('sp500.csv', index_col='Date', parse_dates=['Date'])

data['SMA'] = data['SP500'].rolling(50).mean()

data.plot()
The output:

Error:
KeyError Traceback (most recent call last) ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2645 try: -> 2646 return self._engine.get_loc(key) 2647 except KeyError: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'SP500' During handling of the above exception, another exception occurred: KeyError Traceback (most recent call last) <ipython-input-2-b57075f65742> in <module> 5 data = pd.read_csv('sp500.csv', index_col='Date', parse_dates=['Date']) 6 ----> 7 data['SMA'] = data['SP500'].rolling(50).mean() 8 9 data.plot() ~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key) 2798 if self.columns.nlevels > 1: 2799 return self._getitem_multilevel(key) -> 2800 indexer = self.columns.get_loc(key) 2801 if is_integer(indexer): 2802 indexer = [indexer] ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance) 2646 return self._engine.get_loc(key) 2647 except KeyError: -> 2648 return self._engine.get_loc(self._maybe_cast_indexer(key)) 2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance) 2650 if indexer.ndim > 1 or indexer.size > 1: pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'SP500'
I originally installed python 3.8 using Anaconda, wherein the author said to use the Navigator to install a 3.5 Python environment to be able to run and edit most of his examples. This wasn't even an option in the environment settings. I then downloaded 3.5 and tried using pip to manually install the libraries required ie numpy, pandas, matplotlib and of course found out it was installing them to the oldass 3.8 directories. I uninstalled that and confirmed that it did seem to then install into the 3.5 directories, which I had changed the path for in the Windows 10 Environment / Path settings.

Also I have my .csv file plastered all over the place in downloads and in the python35 directory.

Trying to install base or index resulted in really strange paragraph-long errors in red, that hopefully aren't relevant to this.

This is just the first "simple" example in the python coding book for trade backtesting!!! Already spent hours on this and can't figure out what the heck to do next....any ideas? I have No desire to install or play with Linux I have too many tools that likely won't work right plus I am not trying to relearn an OS..thanks for any advice, pointers or support in advance. I just want a working python version that perhaps has some way like Anaconda to manage and edit the code in a format where it's not line-by-line as the command line python.exe in 3.5 seems to be, also.

Let me know if there's anything else I need to make clear or include to be able to get some advice on this. If this is confusing I've already spent about 5-6 hours banging my head into different walls, help me find a way forward here if you can guys, please.
It seems that the file is read successfully; However, columns were not recognized; consider setting the sep parameter in read_csv. What symbol is used as a separator in csv-file?
It is a set of word/values, separated by commas.

This is the file opened with notepad https://ibb.co/dbqMz3h

Any idea what I'm doing wrong/not doing? P.S. those are not the values for the SP500 they are of a particular stock, but I don't think that matters at all hmm, since the filename is named sp500.csv
According to the image available through the link you provided, valid column names are : Date, Open, High, Low, Close, Adj Close, Volume. There are no columns named 'SP500' or 'SMA'. So, Pandas told this to you by raising the "KeyError" exception.
Note, that you need to put column name in square brackets: data['existed column name'].