1

I am using Quandl to download daily NAV prices for a specific set of Mutual Fund schemes. However it returns a data object instead of returning the specific value

import quandl
import pandas as pd

quandl.ApiConfig.api_key = <Quandl Key>

list2 = [102505, 129221, 102142, 103197, 100614, 100474, 102913, 102921]

def get_nav(mf_code):
    df_main=pd.DataFrame()
    code=str(mf_code)
    df_main=quandl.get("AMFI/"+code,start_date='2019-04-05',end_date='2019-04- 05')
    return (df_main['Net Asset Value'])


for each in list2:
    mf_code=each
    nav = get_nav(mf_code)
    print (nav)

Output for the above code :

Date
2019-04-05    29.8916
Name: Net Asset Value, dtype: float64
Date
2019-04-05    19.354
Name: Net Asset Value, dtype: float64

whereas,

I am looking to extract only the values i.e. 29.8916, 19.354, etc

Updated code:

def get_nav(mf_code):
    nav1=[]
    df_main=pd.DataFrame()
    code=str(mf_code)
#    try:
    df_main=quandl.get("AMFI/"+code,start_date='2019-04-05',end_date='2019-04-05')
    nav_value=df_main['Net Asset Value']
    if not nav_value.empty:
        nav1=nav_value[0]
        print(nav1)
#   print(df_main.head())
#    except IndexError:
#        nav_value=0
    return (nav1)


#Use merged sheet for work
df_port=pd.read_excel(fp_out)
df_port['Current Price']=df_port['Scheme_Code'].apply(lambda x:get_nav(x))

print(df_port['Current Price'].head())
df_port.to_excel(fp_out2)

1 Answers1

1

By default, quandl Time-series API returns you a dataframe with date as index, even if there is only one row.

If you only need the value of first row, you can use iloc:

if not nav.empty:
    print (nav.iloc[0])

or just plain integer indexing:

if not nav.empty:
    print (nav[0])
Aditya Santoso
  • 1,031
  • 6
  • 19
  • IndexError: single positional indexer is out-of-bounds I am getting this error, which fails to print the last value in the list. Unable to get my head around it. – Viraj Desai Apr 12 '19 at 17:10
  • did you check for empty dataframe like in my answer? If the dataframe is not empty `.iloc[0]` should not fail ... – Aditya Santoso Apr 13 '19 at 07:01
  • Updated code pasted above. I am using .empty, however the last value of the list gets me a '[]' in the excel sheet I am generating ( indicating it does not solve the problem entirely) – Viraj Desai Apr 13 '19 at 12:58
  • Quite surprising - but the API is somehow returning an empty dataframe for the respective scheme code - which seems to be a data quality issue, and nothing related to the code! Thanks for all your help! PS : If in case you want to test it - Scheme code is :141143 – Viraj Desai Apr 13 '19 at 13:28