Skip to content

Commit 004ee3c

Browse files
Kurt Forresterbashtage
authored andcommitted
added freq parameter to enable explicit querying of monthly, quarterly and annual data from World Bank.
also added test for monthly and quarterly.
1 parent ffc8ee1 commit 004ee3c

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

pandas_datareader/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ def __init__(self, symbols, start=None, end=None, retry_count=3,
4747
start, end = _sanitize_dates(start, end)
4848
self.start = start
4949
self.end = end
50-
self.freq = freq
5150

5251
if not isinstance(retry_count, int) or retry_count < 0:
5352
raise ValueError("'retry_count' must be integer larger than 0")

pandas_datareader/tests/test_wb.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,65 @@ def test_wdi_get_indicators(self):
214214
# assert_index_equal doesn't exists
215215
assert result.columns.equals(exp_col)
216216
assert len(result) > 10000
217+
218+
def test_wdi_download_monthly(self):
219+
220+
expected = {'COPPER': {('World', '2012M01'): 8040.47,
221+
('World', '2011M12'): 7565.48,
222+
('World', '2011M11'): 7581.02,
223+
('World', '2011M10'): 7394.19,
224+
('World', '2011M09'): 8300.14,
225+
('World', '2011M08'): 9000.76,
226+
('World', '2011M07'): 9650.46,
227+
('World', '2011M06'): 9066.85,
228+
('World', '2011M05'): 8959.90,
229+
('World', '2011M04'): 9492.79,
230+
('World', '2011M03'): 9503.36,
231+
('World', '2011M02'): 9867.60,
232+
('World', '2011M01'): 9555.70}}
233+
expected = pd.DataFrame(expected)
234+
# Round, to ignore revisions to data.
235+
expected = np.round(expected, decimals=-3)
236+
expected = expected.sort_index()
237+
cntry_codes = 'ALL'
238+
inds = 'COPPER'
239+
result = download(country=cntry_codes, indicator=inds,
240+
start=2011, end=2012, freq='M',errors='ignore')
241+
result = result.sort_index()
242+
result = np.round(result, decimals=-3)
243+
244+
expected.index.names = ['country', 'year']
245+
tm.assert_frame_equal(result, expected)
246+
247+
result = WorldBankReader(inds, countries=cntry_codes,
248+
start=2011, end=2012, freq='M', errors='ignore').read()
249+
result = result.sort_index()
250+
result = np.round(result, decimals=-3)
251+
tm.assert_frame_equal(result, expected)
252+
253+
def test_wdi_download_quarterly(self):
254+
255+
expected = {'DT.DOD.PUBS.CD.US': {('Albania', '2012Q1'): 3240539817.18,
256+
('Albania', '2011Q4'): 3213979715.15,
257+
('Albania', '2011Q3'): 3187681048.95,
258+
('Albania', '2011Q2'): 3248041513.86,
259+
('Albania', '2011Q1'): 3137210567.92,}}
260+
expected = pd.DataFrame(expected)
261+
# Round, to ignore revisions to data.
262+
expected = np.round(expected, decimals=-3)
263+
expected = expected.sort_index()
264+
cntry_codes = 'ALB'
265+
inds = 'DT.DOD.PUBS.CD.US'
266+
result = download(country=cntry_codes, indicator=inds,
267+
start=2011, end=2012, freq='Q',errors='ignore')
268+
result = result.sort_index()
269+
result = np.round(result, decimals=-3)
270+
271+
expected.index.names = ['country', 'year']
272+
tm.assert_frame_equal(result, expected)
273+
274+
result = WorldBankReader(inds, countries=cntry_codes,
275+
start=2011, end=2012, freq='Q', errors='ignore').read()
276+
result = result.sort_index()
277+
result = np.round(result, decimals=-1)
278+
tm.assert_frame_equal(result, expected)

pandas_datareader/wb.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __init__(self, symbols=None, countries=None,
125125
symbols = [symbols]
126126

127127
super(WorldBankReader, self).__init__(symbols=symbols,
128-
start=start, end=end, freq=freq,
128+
start=start, end=end,
129129
retry_count=retry_count,
130130
pause=pause, session=session)
131131

@@ -144,6 +144,12 @@ def __init__(self, symbols=None, countries=None,
144144
warnings.warn('Non-standard ISO '
145145
'country codes: %s' % tmp, UserWarning)
146146

147+
freq_symbols = ['M','Q','A', None]
148+
if freq not in freq_symbols:
149+
msg = 'The frequency `{0}` is not in the accepted list.'.format(freq)
150+
raise ValueError(msg)
151+
152+
self.freq = freq
147153
self.countries = countries
148154
self.errors = errors
149155

@@ -159,8 +165,14 @@ def params(self):
159165
return {'date': '{0}M{1:02d}:{2}M{3:02d}'.format(self.start.year,
160166
self.start.month, self.end.year, self.end.month),
161167
'per_page': 25000, 'format': 'json'}
162-
return {'date': '{0}:{1}'.format(self.start.year, self.end.year),
163-
'per_page': 25000, 'format': 'json'}
168+
if self.freq == 'Q':
169+
return {'date': '{0}Q{1}:{2}Q{3}'.format(self.start.year,
170+
divmod(self.start.month-1,3)[0]+1, self.end.year,
171+
divmod(self.end.month-1,3)[0]+1),'per_page': 25000,
172+
'format': 'json'}
173+
if self.freq is None or self.freq == 'A':
174+
return {'date': '{0}:{1}'.format(self.start.year, self.end.year),
175+
'per_page': 25000, 'format': 'json'}
164176

165177
def read(self):
166178
try:
@@ -335,7 +347,7 @@ def search(self, string='gdp.*capi', field='name', case=False):
335347
return out
336348

337349

338-
def download(country=None, indicator=None, start=2003, end=2005,
350+
def download(country=None, indicator=None, start=2003, end=2005, freq=None,
339351
errors='warn', **kwargs):
340352
"""
341353
Download data series from the World Bank's World Development Indicators
@@ -361,6 +373,11 @@ def download(country=None, indicator=None, start=2003, end=2005,
361373
end: int
362374
Last year of the data series (inclusive)
363375
376+
freq: str
377+
frequency or periodicity of the data to be retrieved (e.g. 'M' for
378+
monthly, 'Q' for quarterly, and 'A' for annual). None defaults to
379+
annual.
380+
364381
errors: str {'ignore', 'warn', 'raise'}, default 'warn'
365382
Country codes are validated against a hardcoded list. This controls
366383
the outcome of that validation, and attempts to also apply
@@ -379,7 +396,7 @@ def download(country=None, indicator=None, start=2003, end=2005,
379396
380397
"""
381398
return WorldBankReader(symbols=indicator, countries=country,
382-
start=start, end=end, errors=errors,
399+
start=start, end=end, freq=freq, errors=errors,
383400
**kwargs).read()
384401

385402

0 commit comments

Comments
 (0)