IMF data requests help

91 views
Skip to first unread message

Dori Levanoni

unread,
Nov 12, 2019, 4:41:56 PM11/12/19
to Statistical data and metadata exchange with SDMX and Python
I'm trying to get pandasdmx (v0.9, using pandas 0.23 and python 3.7) up and running for some IMF data requests. In particular, accessing the BOP (BPM6) series.

I feel like I'm truly missing something here, as I suspect the IMF data structures are a bit more complex than the ECB data example in the docs and I'm at a loss on how to proceed. The basic code I'm trying to use is:

#!/usr/local/bin/python
import pandas as pd
import pandasdmx

countries = {'Australia': 'AU', 'Austria': 'OE', 'Belgium': 'BE', 'Canada': 'CA', 'Hong Kong': 'HK', 'Denmark': 'DK', 'Euro area': 'EM', 'Finland': 'FN', 'France': 'FR', 'Germany': 'BD', 'Greece': 'GR
', 'Ireland': 'IR', 'Israel': 'IS', 'Italy': 'IT', 'Japan': 'JP', 'Netherlands': 'NL', 'New Zealand': 'NZ', 'Norway': 'NW', 'Portugal': 'PT', 'Singapore': 'SG', 'Spain': 'ES', 'Sweden': 'SE', 'Switzer
land': 'CH', 'United Kingdom': 'UK', 'United States': 'US'}

imf = pandasdmx.Request('IMF_SDMXCENTRAL')
bop_flow = imf.dataflow('BOP_BPM6')
dsd = bop_flow.dataflow.BOP_BPM6.structure()

# Get a list of regions/areas from country list
areas = bop_flow.write().codelist.loc['CL_REF_AREA']
areas = areas[areas['name'].isin(countries.keys())]

# Get  a list of indicators for each of the specific BOP series of interest
codes = bop_flow.write().codelist.loc['CL_INDICATOR']
codes = codes[codes['name'].str.contains('Balance of Payments', case=False) & codes['name'].str.contains('Financial Account', case=False) & codes['name'].str.contains('Portfolio Investment', case=Fals
e) & codes['name'].str.contains('Net acquisition of financial assets', case=False) & codes['name'].str.contains('US Dollars', case=False)]
codes = codes[codes.index.str.contains('BP6')]

# Pull down data into a dataframe
for code in list(codes.index):
resp = imf.data(resource_id='BOP_BPM6', key={'INDICATOR': code, 'REF_AREA': list(areas)}, dsd=dsd)
data = resp.data
quarterly = (s for s in data.series if s.key.FREQ == 'D')
df = data_response.write(quarterly)

Any help would be appreciated!

Dori Levanoni

unread,
Nov 13, 2019, 1:24:54 PM11/13/19
to Statistical data and metadata exchange with SDMX and Python
There's a small typo in my original post...the last line:
df = data_response.write(quarterly)
should have read:

df = resp.write(quarterly)
But note that the code snipped still doesn't work...

The error I get is:

requests.exceptions.HTTPError: 404 Client Error:  for url: https://sdmxcentral.imf.org/ws/public/sdmxapi/rest/data/BOP_BPM6?detail=serieskeysonly

from this line:

Traceback (most recent call last):
  File "./IMFdata.py", line 59, in <module>
Reply all
Reply to author
Forward
0 new messages