Upgrade from Eikon -> Workspace. Learn about programming differences.

For a deeper look into our Eikon Data API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
135 15 10 10

Getting KeyError: "headers" from data request

Hello! While trying to search for companies in a specific country under a specific TRBC Activity name (industry or sector basically) with the SCREEN function, I get a KeyError: 'headers' from the screen data request:

Belgium = pd.DataFrame()
for item in TRBC_Sectors:
    country = '"BE"'
    activity = item
    exp = 'SCREEN(U(IN(Equity(active,public,private,primary))), IN(TR.RegCountryCode,{}), IN(TR.TRBCActivityCode,{}), CURN=USD)'.format(country,activity)
    fields = ["TR.CommonName","TR.TRBCActivity"]
    output, err = ek.get_data(exp, fields)
    Belgium = Belgium.append(output)


while TRBC_Sectors is a list with strings for each of the TRBC Activity sectors: e.g.

['5010101010', '5010101011', '5010101012', '5010201010', '5010202010', '5010202011', '5010202012', '5010202013', '5010202014', '5010202015', '5010203010', '5010203011', '5010203012', '5010203013', '5010301010', '5010301011', '5010301012', '5010301013', '5010301014']

Error message:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-23-8361b97df6fa> in <module>
      5     exp = 'SCREEN(U(IN(Equity(active,public,private,primary))), IN(TR.RegCountryCode,{}), IN(TR.TRBCActivityCode,{}), CURN=USD)'.format(country,activity)
      6     fields = ["TR.CommonName","TR.TRBCActivity"]
----> 7     output, err = ek.get_data(exp, fields)
      8     Belgium = Belgium.append(output)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\eikon\data_grid.py in get_data(instruments, fields, parameters, field_name, raw_output, debug)
    195         return result
    196 --> 197     return get_data_frame(result, field_name)
    198     199 
~\AppData\Local\Continuum\anaconda3\lib\site-packages\eikon\data_grid.py in get_data_frame(data_dict, field_name)
    244         headers = [header.get('field', header.get('displayName')) for header in data_dict['headers'][0]]
    245     else:
--> 246         headers = [header['displayName'] for header in data_dict['headers'][0]]
    247     data = pd.np.array([[get_data_value(value) for value in row] for row in data_dict['data']])
    248     if len(data):

KeyError: 'headers'
eikoneikon-data-apipythonrefinitiv-dataplatform-eikonworkspaceworkspace-data-apiscreenerkey-error
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

@Alex Putkov could you please advise here?

Upvote
Accepted
22k 58 14 21

Code here:

TRBC_Sectors = ['5010101010', '5010101011', '5010101012', '5010201010', '5010202010', '5010202011', '5010202012', '5010202013', '5010202014', '5010202015', '5010203010', '5010203011', '5010203012', '5010203013', '5010301010', '5010301011', '5010301012', '5010301013', '5010301014']

Belgium = pd.DataFrame()
for item in TRBC_Sectors:
  try:
    print("Trying sector: %s" % item)
    exp = 'SCREEN(U(IN(Equity(active,public,private,primary))), IN(TR.RegCountryCode, "BE"), IN(TR.TRBCActivityCode,{}), CURN=USD)'.format(item)
    output, err = ek.get_data(exp, ["TR.CommonName", "TR.TRBCActivity"])
    print("Received size: %s" % str(output.size))
    Belgium = Belgium.append(output)
  except Exception as exp:
    print("Error getting data for: %s" % item)

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Many thanks for that, Gurpreet, this is very useful.

Upvotes
22k 58 14 21

5010202012 seems to be an invalid TRBC Sector code.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Gurpreet @

No, it is not the problem here. I tried the same screen using TR.TRBCBusinessSectorCode and corresponsing codes (see below) but the error message is the same.

['5010', '5020', '5030', '5110', '5120', '5130', '5210', '5220', '5230', '5240', '5310', '5320', '5330', '5340', '5410', '5420', '5430', '5510', '5560', '5530', '5540', '5550', '5610', '5620', '5710', '5720', '5810', '5910']
Upvotes
135 15 10 10

Many thanks Gurpreet @, there may be many of these invalid TRBC Activity Codes although I received them from Refinitiv Support Desk.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
135 15 10 10

Gurpreet @

No, it is not the problem here. I tried the same screen using TR.TRBCBusinessSectorCode and corresponsing codes (see below) but the error message is the same.

['5010', '5020', '5030', '5110', '5120', '5130', '5210', '5220', '5230', '5240', '5310', '5320', '5330', '5340', '5410', '5420', '5430', '5510', '5560', '5530', '5540', '5550', '5610', '5620', '5710', '5720', '5810', '5910']
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
22k 58 14 21

Just use a Try/Catch block to get past invalid sector codes -

Trying sector: 5010101010
Received size: 60
Trying sector: 5010101011
Received size: 12
Trying sector: 5010101012
Received size: 3
Trying sector: 5010201010
Received size: 15
Trying sector: 5010202010
Received size: 18
Trying sector: 5010202011
Received size: 9
Trying sector: 5010202012
Error getting data for: 5010202012
Trying sector: 5010202013
Error getting data for: 5010202013
Trying sector: 5010202014
Error getting data for: 5010202014
Trying sector: 5010202015
Error getting data for: 5010202015
Trying sector: 5010203010
Received size: 42
Trying sector: 5010203011
Received size: 390
Trying sector: 5010203012
Received size: 552
Trying sector: 5010203013
Received size: 24
Trying sector: 5010301010
Error getting data for: 5010301010
Trying sector: 5010301011
Error getting data for: 5010301011
Trying sector: 5010301012
Error getting data for: 5010301012
Trying sector: 5010301013
Error getting data for: 5010301013
Trying sector: 5010301014
Error getting data for: 5010301014
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.