Getting nan when iterating eikon.get_data

This is the function and iteration:

image

(Tickers.xlsx is a list with 450 RICs)

However when I do this, it works:

image

When I try to put it in a loop, it doesn't want to work. I need to do the above for 457 RICs, really wouldn't want to do it manually one by one. Sorry if the question may be dumb, I am a beginner! Thank you in advance for the responses.

Best Answer

  • I think the easiest way to get what you want is to retrieve all the fields for all the stocks in one call:

    rics = ['SINCH.ST','INVEb.ST','ADDTb.ST','AAK.ST']
    ek.get_data(rics,
                ['TR.CompanyName','TR.HeadquartersCountry','TR.ICBIndustry',
                 'TR.ICBSuperSector','TR.ICBSubSector','TR.TotalAssets',
                 'TR.TotalLiabilities','TR.CompanyMarketCap'],
                {'CURN':'EUR'})

    But if for whatever reason you need to retrieve the data in a loop (one or several stocks at a time), then you need to correctly concatenate the results, which is where the problem is in your code. E.g.

    def get_main_data(ric):
        df, err = ek.get_data(ric,
                              ['TR.CompanyName','TR.HeadquartersCountry',
    'TR.ICBIndustry','TR.ICBSuperSector',
    'TR.ICBSubSector','TR.TotalAssets',
                              'TR.TotalLiabilities','TR.CompanyMarketCap'],
                             {'CURN':'EUR'})
        df.set_index('Instrument', inplace=True)
        return df

    df = pd.DataFrame()
    for ric in ['SINCH.ST','INVEb.ST','ADDTb.ST','AAK.ST']:
        df = df.append(get_main_data(ric))
    df

    Finally, if you'd like the resulting dataframe to have field names as rows and stock RICs as columns, use pandas transpose method:

    df.transpose()