-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimfapi3.html
204 lines (192 loc) · 9.74 KB
/
imfapi3.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
198
199
200
201
202
203
204
<!DOCTYPE html>
<html>
<head>
<meta id="meta" name="viewport" content="width=device-width, initial-scale=1.0" />
<title> BD Economics | IMF API Guide, Part 3 </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>
<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_dull">
<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">
<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: Metadata and advanced requests </h3>
<p>In part 3, we obtain the metadata related to our request from part 1 and show how to make a slightly more advanced request, which includes more than one element in a dimension. Specifically, we calculate the U.K.'s share of goods imports that originate in the EU.</p>
<h3>Obtaining the metadata</h3>
<p>Part 2 explains how to generalize the example in part 1 by finding the codes which correspond to our data of interest and return successful API requests. However, the <code>CompactData</code> method used in part 1 does not return any metadata, such as full country names, the units of measure, or the indicator name.</p>
<p>Metadata are obtained by using the <code>GenericMetadata</code> method, demonstrated for the U.K. import price example from part 1 as follows:</p>
<p>In[1]:</p>
<pre><code class="python">import requests # Python 3.6
url = 'http://dataservices.imf.org/REST/SDMX_JSON.svc/'
key = 'GenericMetadata/IFS/M.GB.PMP_IX'
metadata = requests.get(f'{url}{key}').json()
country = metadata['GenericMetadata']['MetadataSet']\
['AttributeValueSet'][1]['ReportedAttribute']\
[1]['ReportedAttribute'][3]['Value']['#text']
indicator = metadata['GenericMetadata']['MetadataSet']\
['AttributeValueSet'][2]['ReportedAttribute']\
[1]['ReportedAttribute'][4]['Value']['#text']
print(f'Country: {country}; Indicator: {indicator}')</code></pre>
<p>Out[1]:</p>
<pre>Country: United Kingdom;
Indicator: Import Price Index, All Commodities</pre>
<p>The <code>GenericMetadata</code> method returns several options for country and indicator names, so it may be helpful to step into the JSON data and examine what is available directly. </p>
<h3> More complex requests </h3>
<p>Perhaps we are interested in the share of goods the U.K. imports from the European Union, which requires information on both trade between the U.K and the EU, and on total U.K. trade. The API allows you to combine along a single dimension by adding '+'. For example, searching for the dimension 4 (<code>CL_COUNTERPART_AREA_DOT</code>) code using the technique described in part 2 returns <code>B0</code> as the code for the EU, and <code>W00</code> as the code for the world (total trade). </p>
<p>Pandas is used for calculations and to make a simple line plot of the result.</p>
<p>In[2]:</p>
<pre><code class="python">import pandas as pd # pandas version 0.22
# key includes two partners, B0 and W00 for EU and world
key = 'CompactData/DOT/M.GB.TMG_CIF_USD.B0+W00'
# Retrieve data from IMF API
data = requests.get(f'{url}{key}').json()
# Convert results to pandas dataframe
df = pd.DataFrame({s['@COUNTERPART_AREA'] : {pd.to_datetime(i['@TIME_PERIOD']) :
round(float(i['@OBS_VALUE']), 1) for i in s['Obs']}
for s in data['CompactData']['DataSet']['Series']})
# 12 month moving average of EU share of total
eu_share = (df['B0'].div(df['W00']) * 100).rolling(12).mean()
# Create a line plot and print most recent value as x label
title = "U.K. imports of goods: European Union share of total"
recent = f"{eu_share.index[-1].strftime('%B %Y')}: {eu_share[-1].round(1)}%"
ax = eu_share.plot(title=title)
ax = ax.set_xlabel(recent)</code></pre>
<p>Out[2]:</p>
<img src="images/uk_im.png" alt="Pandas Plot Output" style="max-width: 100%;"/>
<h3> Additional resources </h3>
<p> IMF API documentation:<br>
The <a href="http://datahelp.imf.org/knowledgebase/articles/630877-data-services">Data Services</a> section of the IMF's website offers useful guidance on the APIs, and has a separate <a href="http://data.imf.org/?sk=A329021F-1ED6-4D6E-B719-5BF5413923B6">news</a> section, which is a good place to check if something breaks. Finally, some guidance on handling large queries is available here:</p>
<p>Learning python for exploratory data analysis:<br>
<ul>
<li> Introductory level: <a href="https://www.datacamp.com/courses/intro-to-python-for-data-science"> Data Camp Intro to Python for Data Science </a>
<li> Intermediate level: <a href="https://www.youtube.com/watch?v=w26x-z-BdWQ"> Data analysis in Python with pandas </a> (Wes McKinney at pycon 2012)
</ul>
</p>
<p>R package by Mingjer Lee to access the IMF API: https://github.com/mingjerli/IMFData </p>
</article>
<div class="subfooter">
<a href="imfapi2.html"> « Back (Part 2)</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>