0

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
1
  • Hello and welcome to SO. What exactly is the question here? How to use exception handling in general? You will find plenty of tutorials either here on SO (e.g. this ) or on the web by searching for Python exception handling. In general you need a try and except clause, in your example it would be try:, then the indented code you want to execute, followed by an except ValueError: pass, or similar. Commented Jul 5, 2022 at 7:21

1 Answer 1

0

This is a key error, it will be helpful to understand what kind of object is k[s] before error handling. I suppose you are looking to grab values from the rows of the dataframe rather than columns and hence when you call columns as 0,1,2,3 it is showing key error. But if you still want just to handle this error below code should do it:

from numpy.core.fromnumeric import var
from datetime import date
counter=0
for symbol in relevant:  
    if len(k[s])>0:   
      try:
        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])
      except:
          pass

If there is an error then your variable varH,varL,varC will not be initialized, so you need to think about that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.