9
9
import numpy as np
10
10
import warnings
11
11
12
+ warnings .warn ("\n "
13
+ "The pandas.io.wb module is moved to a separate package "
14
+ "(pandas-datareader) and will be removed from pandas in a "
15
+ "future version.\n After installing the pandas-datareader package "
16
+ "(https://github.com/pydata/pandas-datareader), you can change "
17
+ "the import ``from pandas.io import data, wb`` to "
18
+ "``from pandas_datareader import data, wb``." ,
19
+ FutureWarning )
20
+
21
+
12
22
# This list of country codes was pulled from wikipedia during October 2014.
13
23
# While some exceptions do exist, it is the best proxy for countries supported
14
- # by World Bank. It is an aggregation of the 2-digit ISO 3166-1 alpha-2, and
15
- # 3-digit ISO 3166-1 alpha-3, codes, with 'all', 'ALL', and 'All' appended ot
24
+ # by World Bank. It is an aggregation of the 2-digit ISO 3166-1 alpha-2, and
25
+ # 3-digit ISO 3166-1 alpha-3, codes, with 'all', 'ALL', and 'All' appended ot
16
26
# the end.
17
27
18
28
country_codes = ['AD' , 'AE' , 'AF' , 'AG' , 'AI' , 'AL' , 'AM' , 'AO' , 'AQ' , 'AR' , \
@@ -83,35 +93,35 @@ def download(country=['MX', 'CA', 'US'], indicator=['NY.GDP.MKTP.CD', 'NY.GNS.IC
83
93
84
94
indicator: string or list of strings
85
95
taken from the ``id`` field in ``WDIsearch()``
86
-
96
+
87
97
country: string or list of strings.
88
98
``all`` downloads data for all countries
89
99
2 or 3 character ISO country codes select individual
90
100
countries (e.g.``US``,``CA``) or (e.g.``USA``,``CAN``). The codes
91
101
can be mixed.
92
-
102
+
93
103
The two ISO lists of countries, provided by wikipedia, are hardcoded
94
104
into pandas as of 11/10/2014.
95
-
105
+
96
106
start: int
97
107
First year of the data series
98
-
108
+
99
109
end: int
100
110
Last year of the data series (inclusive)
101
-
111
+
102
112
errors: str {'ignore', 'warn', 'raise'}, default 'warn'
103
113
Country codes are validated against a hardcoded list. This controls
104
114
the outcome of that validation, and attempts to also apply
105
115
to the results from world bank.
106
-
116
+
107
117
errors='raise', will raise a ValueError on a bad country code.
108
-
118
+
109
119
Returns
110
120
-------
111
121
112
- ``pandas`` DataFrame with columns: country, iso_code, year,
122
+ ``pandas`` DataFrame with columns: country, iso_code, year,
113
123
indicator value.
114
-
124
+
115
125
"""
116
126
117
127
if type (country ) == str :
@@ -130,7 +140,7 @@ def download(country=['MX', 'CA', 'US'], indicator=['NY.GDP.MKTP.CD', 'NY.GNS.IC
130
140
# Work with a list of indicators
131
141
if type (indicator ) == str :
132
142
indicator = [indicator ]
133
-
143
+
134
144
# Download
135
145
data = []
136
146
bad_indicators = {}
@@ -165,12 +175,12 @@ def download(country=['MX', 'CA', 'US'], indicator=['NY.GDP.MKTP.CD', 'NY.GNS.IC
165
175
166
176
def _get_data (indicator = "NY.GNS.ICTR.GN.ZS" , country = 'US' ,
167
177
start = 2002 , end = 2005 ):
168
-
178
+
169
179
if type (country ) == str :
170
180
country = [country ]
171
-
181
+
172
182
countries = ';' .join (country )
173
-
183
+
174
184
# Build URL for api call
175
185
url = ("http://api.worldbank.org/countries/" + countries + "/indicators/" +
176
186
indicator + "?date=" + str (start ) + ":" + str (end ) +
@@ -195,11 +205,11 @@ def _get_data(indicator="NY.GNS.ICTR.GN.ZS", country='US',
195
205
wb_err += msg ['value' ]
196
206
error_msg = "Problem with a World Bank Query \n %s"
197
207
return None , error_msg % wb_err
198
-
208
+
199
209
if 'total' in possible_message .keys ():
200
210
if possible_message ['total' ] == 0 :
201
211
return None , "No results from world bank."
202
-
212
+
203
213
# Parse JSON file
204
214
data = json .loads (data )[1 ]
205
215
country = [x ['country' ]['value' ] for x in data ]
@@ -296,4 +306,3 @@ def search(string='gdp.*capi', field='name', case=False):
296
306
idx = data .str .contains (string , case = case )
297
307
out = _cached_series .ix [idx ].dropna ()
298
308
return out
299
-
0 commit comments