Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Python
def get_stock_data(stock, start_date, end_date):
    data = breeze.get_historical_data_v2 (interval="1day",
                            from_date= start_date,
                            to_date= end_date,
                            stock_code=stock,
                            exchange_code="NSE",
                            product_type="cash")
    df1 = pd.DataFrame(data['Success'])
    return data


What I have tried:

So the key error is:
KeyError
Traceback (most recent call last)
Cell In[40], line 1
----> 1 df = get_stock_data('TCS','2014-11-1','2023-10-1')

Cell In[39], line 10, in get_stock_data(stock, start_date, end_date)
      2 data = breeze.get_historical_data_v2 (interval="1day",
      3                         from_date= start_date,
      4                         to_date= end_date,
      5                         stock_code=stock,
      6                         exchange_code="NSE",
      7                         product_type="cash")
      8 #data_dict = response.json()
      9 #df1 = pd.DataFrame(data_dict)
---> 10 df1 = pd.DataFrame(data['Success'])
     11 #data = pd.DataFrame(data(index_cols=0,parse=True))
     12 ##df = pd.DataFrame.from_dict(data)
     15 return df

KeyError: 'Success'
Posted
Updated 6-Nov-23 4:11am
v3

1 solution

A Key Error occurs when you try to access a column in the data, but it doesn't exist.

We can't fix that for you: we have no idea what the API returns.
So start with the debugger (pdb — The Python Debugger — Python 3.12.0 documentation[^]) and look at the actual data you are processing, work out what the column names are, and compare those with your code. At a guess, "Success" is the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900