raise KeyError(key) Python - python

I get this error when executing my code do you have an idea how to fix it? here is the image and the code of the first lines of my csv file, the file is too big to display it entirely. the code is complete. i try to display the points on a map with geoplotlib
enter image description here
import geoplotlib
import csv
import pandas as pd
data = pd.read_csv('E:\Projet de Memoir sur les tics/humdataAL.csv')
geoplotlib.dot(data)
geoplotlib.show()
It returns the following error:
Traceback (most recent call last):
File "C:\Users\nsama\PycharmProjects\pythonProject\amara_project\venv\lib\site-packages\geoplotlib\__init__.py", line 32, in _runapp
app.start()
File "C:\Users\nsama\PycharmProjects\pythonProject\amara_project\venv\lib\site-packages\geoplotlib\core.py", line 364, in start
self.proj.fit(BoundingBox.from_bboxes([l.bbox() for l in self.geoplotlib_config.layers]),
File "C:\Users\nsama\PycharmProjects\pythonProject\amara_project\venv\lib\site-packages\geoplotlib\core.py", line 364, in <listcomp>
self.proj.fit(BoundingBox.from_bboxes([l.bbox() for l in self.geoplotlib_config.layers]),
File "C:\Users\nsama\PycharmProjects\pythonProject\amara_project\venv\lib\site-packages\geoplotlib\layers.py", line 159, in bbox
return BoundingBox.from_points(lons=self.data['lon'], lats=self.data['lat'])
File "C:\Users\nsama\PycharmProjects\pythonProject\amara_project\venv\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\nsama\PycharmProjects\pythonProject\amara_project\venv\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
raise KeyError(key) from err
KeyError: 'lon'

Your CSV doesn't meet geoplotlib specifications. In particular, all rows must have columns named lat and lon in dataset.

Related

How do I handle a date error in eventstudy package?

I'm trying to run multiple event studies in Python with the eventstudy package. Still, I keep getting the exact date error no matter what I do (different date formats, naming or not the columns, setting the date_format parameter,...)
Would you know what is wrong or how else I could do these multiple event studies?
This is my code now:
import eventstudy as es
import pandas as pd
returns = "C:/Users/Artur Andrade/OneDrive/Documents/_Others/Monografia/Eu/Base de dados/RETURNS.csv"
events = "C:/Users/Artur Andrade/OneDrive/Documents/_Others/Monografia/Eu/Base de dados/TESTE.csv"
es.Single.import_returns("C:/Users/Artur Andrade/OneDrive/Documents/_Others/Monografia/Eu/Base de dados/RETURNS.csv", is_price=True)
energy = es.Multiple.from_csv(
path = "C:/Users/Artur Andrade/OneDrive/Documents/_Others/Monografia/Eu/Base de dados/TESTE.csv",
event_study_model = es.Single.market_model,
event_window = (-5,+10),
estimation_size = 100,
buffer_size = 30,
ignore_errors = True
)
energy.results()
energy.plot()
And I keep getting this error:
Traceback (most recent call last):
File "C:\Users\Artur Andrade\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'date'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Artur Andrade\OneDrive\Documents\_Others\Monografia\Eu\Base de dados\codigo\evento.py", line 11, in <module>
es.Single.import_returns("C:/Users/Artur Andrade/OneDrive/Documents/_Others/Monografia/Eu/Base de dados/RETURNS.csv", is_price=True)
File "C:\Users\Artur Andrade\AppData\Local\Programs\Python\Python310\lib\site-packages\eventstudy\single.py", line 327, in import_returns
data = read_csv(path, format_date=True, date_format=date_format)
File "C:\Users\Artur Andrade\AppData\Local\Programs\Python\Python310\lib\site-packages\eventstudy\utils.py", line 112, in read_csv
df[date_column] = pd.to_datetime(df[date_column], format=date_format)
File "C:\Users\Artur Andrade\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3505, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\Artur Andrade\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc
raise KeyError(key) from err
KeyError: 'date'
My database right now is just like this:
RETURNS.csv:
Date,IBOV,IEE,AESB3,EGIE3,ENBR3,EQTL3,MEGA3,NEOE3,ENEV3
2017-01-02,"59,589","36,062",14, ,11.18,10.53, , ,2.97
.
.
.
2022-09-23,"111,716","82,969",9.64,40.7,23.71,26.97,10.72,16.48,15.34
2022-09-26,"109,114","80,913",9.6,39.7,23.16,26.71,10.42,16.11,14.81
2022-09-27,"108,376","79,121",9.55,39.07,22.72,26.15,10.36,15.72,14.37
2022-09-28,"108,451","78,427",9.44,38.54,22.04,26.1,10.59,15.35,14.26
2022-09-29,"107,664","77,950",9.37,38.29,21.72,26.01,10.38,15.23,14.45
TESTE.csv:
security_ticker,market_ticker,event_date
AESB3,IBOV,2017-01-13
.
.
.
ENBR3,IBOV,2022-04-20
EQTL3,IBOV,2021-06-02
MEGA3,IBOV,2022-07-04
ENEV3,IBOV,2021-12-15

Key error message when calculating variables using pandas and yfinance

trying to calculate some variables from yfinance from the column df['Close'].
But im getting this error which i have not seen before. and heres are the code:
import os
import pandas as pd
import plotly.graph_objects as go
symbols = 'AAPL'
for filename in os.listdir('datasets/'):
#print(filename)
symbol = filename.split('.')[0]
#print(symbol)
df = pd.read_csv('datasets/{}'.format(filename))
if df.empty:
continue
df['20_sma'] = df['Close'].rolling(window=20).mean()
df['stddev'] = df['Close'].rolling(window=20).std()
df['lowerband'] = df['20_sma'] + (2* df['stddev'])
df['upperband'] = df['20_sma'] - (2* df['stddev'])
if symbol in symbols:
print(df)
and heres are the error message:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 2895, in get_loc
return self._engine.get_loc(casted_key)
File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Close'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/Kit/Documents/TTM_squeezer/squeeze.py", line 16, in <module>
df['20_sma'] = df['Close'].rolling(window=20).mean()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/frame.py", line 2906, in __getitem__
indexer = self.columns.get_loc(key)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexes/base.py", line 2897, in get_loc
raise KeyError(key) from err
KeyError: 'Close'
Seems like the 'Close' column has contributed to this error but i just cant figure out why?
Many thanks
turns out there was an error in the process where the local file was saved
case closed, thanks all

Pandas Dataframe: "ValueError: could not convert string to float" when trying to change value of second column entry

I have a short code where I want to be able to change values of a csv.
My Code is the following:
import pandas as
import os
if os.path.exists('annotation.csv'):
df = pd.read_csv('annotation.csv')
label = 'unknown'
img_number = 99
if str(df.at[img_number, 'label_2']) == 'nan':
df.at[img_number, 'label_2'] = label
else:
continue
My file looks like this:
label_1,label_2
,
,
,
,
,
(and so on)
I am able to change 'label_1' in df.at[img_number, 'label_2']
but if I try replace it 'label_2' I get the following error.
Traceback (most recent call last):
File "D:/develop/mbuchwald/machinelearning/Organ_Annotation/test.py", line 11, in <module>
df.at[img_number, 'label_2'] = label
File "C:\Users\mbuchwald\AppData\Local\Continuum\anaconda3\envs\newEnv\lib\site-packages\pandas\core\indexing.py", line 2159, in __setitem__
self.obj._set_value(*key, takeable=self._takeable)
File "C:\Users\mbuchwald\AppData\Local\Continuum\anaconda3\envs\newEnv\lib\site-packages\pandas\core\frame.py", line 2582, in _set_value
engine.set_value(series._values, index, value)
File "pandas\_libs\index.pyx", line 124, in pandas._libs.index.IndexEngine.set_value
File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.set_value
File "pandas/_libs/src\util.pxd", line 150, in util.set_value_at
File "pandas/_libs/src\util.pxd", line 142, in util.set_value_at_unsafe
ValueError: could not convert string to float: 'unknown'
Has anyone a clue. I canĀ“t figure it out. Thank you!

TypeError: expected string or bytes-like object while trying to plot data from a dataframe

"""
I am trying to plot a graph from data retrieved from a CSV file. The code is as below
"""
path = '/Users/pradeepallath/Documents/000_Edureka_Training/001_PredictiveAnalysis/Weather_WWII'
import pandas as pd
dataset = pd.read_csv(path+'/Weather.csv',low_memory=False,nrows=1000)
dataset.plot(x='MinTemp',y='MaxTemp',style=0)
plt.plot()
"""
I am Getting this error. Please note I am new to Python
Traceback (most recent call last):
File "/Users/pradeepallath/Documents/Pycharm/Big_Mart_Sale/Mean_Sale.py", line 13, in <module>
dataset.plot(x='MinTemp',y='MaxTemp',style=0)
File "/Users/pradeepallath/anaconda3/lib/python3.7/site-packages/pandas/plotting/_core.py", line 794, in __call__
return plot_backend.plot(data, kind=kind, **kwargs)
File "/Users/pradeepallath/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/__init__.py", line 62, in plot
plot_obj.generate()
File "/Users/pradeepallath/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/core.py", line 281, in generate
self._make_plot()
File "/Users/pradeepallath/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/core.py", line 1063, in _make_plot
style, kwds = self._apply_style_colors(colors, kwds, i, label)
File "/Users/pradeepallath/anaconda3/lib/python3.7/site-packages/pandas/plotting/_matplotlib/core.py", line 723, in _apply_style_colors
nocolor_style = style is None or re.match("[a-z]+", style) is None
File "/Users/pradeepallath/anaconda3/lib/python3.7/re.py", line 173, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or bytes-like object
Thanks for the assistance
"""
It's taking issue with style=0.
Pandas supports matplotlib line styles.
Here is a good stackoverflow question on how you can see valid options, but essentially the integer zero is not a valid line style.

Shorten large stack traces when using libraries

I work very often with large libraries like pandas, or matplotlib.
This means that exceptions often produce long stack traces.
Since the error is extremely rarely with the library, and extremely often with my own code, I don't need to see the library detail in the vast majority of cases.
A couple of common examples:
Pandas
>>> import pandas as pd
>>> df = pd.DataFrame(dict(a=[1,2,3]))
>>> df['b'] # Hint: there _is_ no 'b'
Here I've attempted to access an unknown key. This simple error produces a stacktrace containing 28 lines:
Traceback (most recent call last):
File "an_arbitrary_python\lib\site-packages\pandas\core\indexes\base.py", line 2393, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)
File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)
File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)
KeyError: 'b'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "an_arbitrary_python\lib\site-packages\pandas\core\frame.py", line 2062, in __getitem__
return self._getitem_column(key)
File "an_arbitrary_python\lib\site-packages\pandas\core\frame.py", line 2069, in _getitem_column
return self._get_item_cache(key)
File "an_arbitrary_python\lib\site-packages\pandas\core\generic.py", line 1534, in _get_item_cache
values = self._data.get(item)
File "an_arbitrary_python\lib\site-packages\pandas\core\internals.py", line 3590, in get
loc = self.items.get_loc(item)
File "an_arbitrary_python\lib\site-packages\pandas\core\indexes\base.py", line 2395, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239)
File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085)
File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405)
File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359)
KeyError: 'b'
Knowing that I ended up in hashtable_class_helper.pxi is almost never helpful for me. I need to know where in my code I've messed up.
Matplotlib
>>> import matplotlib.pyplot as plt
>>> import matplotlib.cm as cm
>>> def foo():
... plt.plot([1,2,3], cbap=cm.Blues) # cbap is a typo for cmap
...
>>> def bar():
... foo()
...
>>> bar()
This time, there's a typo in my keyword argument. But I still have to see 25 lines of stack trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in bar
File "<stdin>", line 2, in foo
File "an_arbitrary_python\lib\site-packages\matplotlib\pyplot.py", line 3317, in plot
ret = ax.plot(*args, **kwargs)
File "an_arbitrary_python\lib\site-packages\matplotlib\__init__.py", line 1897, in inner
return func(ax, *args, **kwargs)
File "an_arbitrary_python\lib\site-packages\matplotlib\axes\_axes.py", line 1406, in plot
for line in self._get_lines(*args, **kwargs):
File "an_arbitrary_python\lib\site-packages\matplotlib\axes\_base.py", line 407, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "an_arbitrary_python\lib\site-packages\matplotlib\axes\_base.py", line 395, in _plot_args
seg = func(x[:, j % ncx], y[:, j % ncy], kw, kwargs)
File "an_arbitrary_python\lib\site-packages\matplotlib\axes\_base.py", line 302, in _makeline
seg = mlines.Line2D(x, y, **kw)
File "an_arbitrary_python\lib\site-packages\matplotlib\lines.py", line 431, in __init__
self.update(kwargs)
File "an_arbitrary_python\lib\site-packages\matplotlib\artist.py", line 885, in update
for k, v in props.items()]
File "an_arbitrary_python\lib\site-packages\matplotlib\artist.py", line 885, in <listcomp>
for k, v in props.items()]
File "an_arbitrary_python\lib\site-packages\matplotlib\artist.py", line 878, in _update_property
raise AttributeError('Unknown property %s' % k)
AttributeError: Unknown property cbap
Here I get to find out that I ended on a line in artist.py that raises an AttributeError, and then see directly underneath that the AttributeError was indeed raised. This is not much value add in information terms.
In these trivial, interactive examples, you might just say "Look at the top of the stack trace, not the bottom", but often my foolish typo has occurred within a function so the line I'm interested in is somewhere in the middle of these library-cluttered stack traces.
Is there any way I can make these stack traces less verbose, and help me find the source of the problem, which almost always lies with my own code and not in the libraries I happen to be employing?
You can use traceback to have better control over exception printing. For example:
import pandas as pd
import traceback
try:
df = pd.DataFrame(dict(a=[1,2,3]))
df['b']
except Exception, e:
traceback.print_exc(limit=1)
exit(1)
This triggers the standard exception printing mechanism, but only shows you the first frame of the stack trace (which is the one you care about in your example). For me this produces:
Traceback (most recent call last):
File "t.py", line 6, in <module>
df['b']
KeyError: 'b'
Obviously you lose the context, which will be important when debugging your own code. If we want to get fancy, we can try and devise a test and see how far the traceback should go. For example:
def find_depth(tb, continue_test):
depth = 0
while tb is not None:
filename = tb.tb_frame.f_code.co_filename
# Run the test we're given against the filename
if not continue_test(filename):
return depth
tb = tb.tb_next
depth += 1
I don't know how you're organising and running your code, but perhaps you can then do something like:
import pandas as pd
import traceback
import sys
def find_depth():
# ... code from above here ...
try:
df = pd.DataFrame(dict(a=[1, 2, 3]))
df['b']
except Exception, e:
traceback.print_exc(limit=get_depth(
sys.exc_info()[2],
# The test for which frames we should include
lambda filename: filename.startswith('my_module')
))
exit(1)

Categories

Resources