Trying to add an exception to this code. Need to have an exception which will ignore data if it throws the below error message (given below the code)
Code
from numpy.core.fromnumeric import var
from datetime import date
counter=0
for symbol in relevant:
if len(k[s])>0:
varD = (pd.DataFrame(k[s])[0])
varO = (pd.DataFrame(k[s])[1])
varH = (pd.DataFrame(k[s])[2])
varL = (pd.DataFrame(k[s])[3])
varC = (pd.DataFrame(k[s])[4])
print("Symbol","P-H","P-L","P-C","T-H","T-L","CMP")
print(symbol,varH[0],varL[0],varC[0],varH[1],varL[1],varC[1])
Error
ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
384 try:
--> 385 return self._range.index(new_key)
386 except ValueError as err:
ValueError: 1 is not in range
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
3 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
385 return self._range.index(new_key)
386 except ValueError as err:
--> 387 raise KeyError(key) from err
388 raise KeyError(key)
389 return super().get_loc(key, method=method, tolerance=tolerance)
KeyError: 1
tryandexceptclause, in your example it would betry:, then the indented code you want to execute, followed by anexcept ValueError: pass, or similar.