-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimfapi2.html
197 lines (189 loc) · 9.65 KB
/
imfapi2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
<meta id="meta" name="viewport" content="width=device-width, initial-scale=1.0" />
<title> BD Economics | IMF API Guide, Part 2 </title>
<link rel="stylesheet" href="add-ins/github.css">
<script src="add-ins/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Lato:900" rel="stylesheet">
<script src="https://use.fontawesome.com/8f99c5621e.js"></script>
<meta charset="UTF-8">
<meta name="description" content="Tools for Economists">
<meta name="keywords" content="IMF API, Economics Dashboard, Trade Network, Trade Networks, Macroeconomics Dashboard, Macroeconomic Dashboard, Markets Dashboard, Market Dashboard, U.S. Economy, U.S. Economy Dashboard, US economy, US economy dashboard, US economy charts, US economy charts pdf, NetworkX trade, international trade networks, network analysis of trade, International Monetary Fund, IMF Statistics Department, IMF Application Programming Interface, International Monetary Fund Application Programming Interface, International Monetary Fund API">
<meta name="author" content="Brian Dew">
<link rel="apple-touch-icon" sizes="57x57" href="favicon/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="favicon/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="favicon/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="favicon/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="favicon/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="favicon/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="favicon/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="favicon/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="favicon/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="favicon/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="favicon/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="favicon/favicon-16x16.png">
<link rel="manifest" href="favicon/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="favicon/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-2J5HVG07X3"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-2J5HVG07X3');
</script>
</head>
<body>
<nav>
<ul class="ul_nav" id="menu">
<li class="nav_main"> <a href="index.html">BD Economics</a> </li>
<li><a href="about.html">About</a> </li>
<li><a href="http://briandew.wordpress.com">Blog</a> </li>
<li><a href="python.html" class="active">Tools ↓</a>
<ul class="hidden">
<li><a href="imfapi1.html">IMF API</a></li>
<li><a href="blsapi.html">BLS API</a></li>
<li><a href="cps.html">CPS Microdata</a></li>
</ul>
</li>
<li>
<a href="chartbook.html">Reports ↓</a>
<ul class="hidden">
<li><a href="chartbook.pdf">US Chartbook (PDF)</a></li>
</ul>
<li class="icon"> <a href="javascript:void(0);"
style="font-size:15px;" onclick="responsiveNav()">☰</a>
</li>
</ul>
</nav>
<header>
<h3>IMF API</h3>
</header>
<section>
<article class="article_links">
<a href="imfapi1.html">
<div class="card_dull">
<div class="card_head_blue">
<h2>Part 1</h2>
</div>
<div class="card_tail">
<p>A <strong>quick example</strong> of requesting, cleaning up, saving to csv, and then plotting a series from IFS using the API.</p>
</div>
</div>
</a>
<a href="imfapi2.html">
<div class="card">
<div class="card_head_blue">
<h2>Part 2</h2>
</div>
<div class="card_tail">
<p>An example of how to find the <strong>dimensions and codes</strong> which correspond to your data of interest.</p>
</div>
</div>
</a>
<a href="imfapi3.html">
<div class="card_dull">
<div class="card_head_blue">
<h2>Part 3</h2>
</div>
<div class="card_tail">
<p>How to retrieve the <strong>metadata</strong> for your series of interest and how to make more <strong>advanced requests</strong> from the API.</p>
</div>
</div>
</a>
</article>
<article class="article_code">
<h3> IMF API with Python: Finding dimensions and codes </h3>
<p>The example presented in part 1 accesses data from the IMF API. Here in part 2, techniques for finding the right codes to make successful API requests are presented, using the requests package and Python 2.7.</p>
<h3>The series list</h3>
<p>The <code>Dataflow</code> method offers JSON formatted information on which series are available through the API. To find the series of interest (for example Direction of Trade Statistics <code>DOT</code>) with Python, we can search a dictionary with the series names and their IDs.</p>
<p>In[1]:</p>
<pre><code class="python">import requests # Python 3.6
url = 'http://dataservices.imf.org/REST/SDMX_JSON.svc/'
key = 'Dataflow' # Method with series information
search_term = 'Trade' # Term to find in series names
series_list = requests.get(f'{url}{key}').json()\
['Structure']['Dataflows']['Dataflow']
# Use dict keys to navigate through results:
for series in series_list:
if search_term in series['Name']['#text']:
print(f"{series['Name']['#text']}: {series['KeyFamilyRef']['KeyFamilyID']}")</code></pre>
<p>Out[1]:</p><pre>Direction of Trade Statistics (DOTS): DOT</pre>
<h3> Finding the dimensions of the series </h3>
<p>The exact format of the key in our API request is determined by the structure of the series. Direction of Trade Statistics, which are grouped by importer and exporter pair rather than by country exemplifies the need to first determine series structure.</p>
<p>The dimensions of the data are found with the DataStructure method and series specific, so that the full key becomes <code>DataStructure/DOT</code></p>
<p>In[2]:</p>
<pre><code class="python">key = 'DataStructure/DOT' # Method / series
dimension_list = requests.get(f'{url}{key}').json()\
['Structure']['KeyFamilies']['KeyFamily']\
['Components']['Dimension']
for n, dimension in enumerate(dimension_list):
print(f'Dimension {n+1}: {dimension['@codelist']}')</code></pre>
<p>Out[2]:</p>
<pre>Dimension 1: CL_FREQ
Dimension 2: CL_AREA_DOT
Dimension 3: CL_INDICATOR_DOT
Dimension 4: CL_COUNTERPART_AREA_DOT
</pre>
<p>In this case, the dimensions correspond to: 1) frequency, 2) country or reference area 1, 3) indicator (such as total exports), and 4) country or reference area 2. That is, the monthly value of goods exports from Italy to France would be <code>M.IT.TXG_FOB.FR</code></p>
<h3>Finding the codes for each dimension</h3>
<p>The codes which correspond to the dimensions identified above are combined, in order, and separated by periods, to complete the API request url. To find the list of possible codes for each dimension, we can use the CodeList method, shown below for dimension 3, indicators <code>CL_INDICATOR_DOT</code>.</p>
<p>In[3]:</p>
<pre><code class="python"># Example: codes for third dimension, which is 2 in python
key = f"CodeList/{dimension_list[2]['@codelist']}"
code_list = requests.get(f'{url}{key}').json()\
['Structure']['CodeLists']['CodeList']['Code']
for code in code_list:
print(f"{code['Description']['#text']}: {code['@value']}")</code></pre>
<p>Out[3]:</p>
<pre>Goods, Value of Exports, Free on board (FOB), US Dollars: TXG_FOB_USD
Goods, Value of Imports, Cost, Insurance, Freight (CIF), US Dollars: TMG_CIF_USD
Goods, Value of Imports, Free on board (FOB), US Dollars: TMG_FOB_USD
All Indicators: All_Indicators</pre>
<h3>Variations and notes</h3>
<p>Once a series has been identified, it can be a challenge to determine which combination of codes returns valid data. Often an indicator has data available at only one frequency--whichever frequency of compilation occurs in the source country, though there are exceptions.</p>
<p>The number of indicators varies by series. In the case of International Financial Statistics (IFS), there are more than 2500 indicators, while there are four indicators in the Direction of Trade Statistics series. The same search technique used to find the series names can be used to filter IFS indicators.</p>
<p>Part 3 shows how to retrieve metadata and make a request with more than one reference area (country).</p>
</article>
<div class="subfooter">
<a href="imfapi1.html"> « Back (Part 1)</a> | <a href="imfapi3.html">Next (Part 3) »</a>
</div>
</section>
<footer>
<div class="footer_left">
<p>March 11, 2018<br> by Brian Dew</p>
</div>
<div class="footer_right">
<a href="https://github.com/bdecon/">
<button class="button_sm"><i class="fa fa-github"></i></button>
</a>
<a href="https://www.linkedin.com/in/brian-dew-5788a386/">
<button class="button_sm"><i class="fa fa-linkedin"></i></button>
</a>
<a href="https://twitter.com/bd_econ">
<button class="button_sm"><i class="fa fa-twitter"></i></button>
</a>
<a href="https://briandew.wordpress.com/">
<button class="button_sm"><i class="fa fa-wordpress"></i></button>
</a>
</div>
</footer>
<script>
function responsiveNav() {
var x = document.getElementById("menu");
if (x.className === "ul_nav") {
x.className += " responsive";
} else {
x.className = "ul_nav";
}
}
</script>
</body>
</html>