|
1 | 1 | import time |
2 | | -import pytest |
3 | 2 |
|
4 | 3 | import numpy as np |
5 | 4 | import pandas as pd |
| 5 | +import pandas.util.testing as tm |
| 6 | +import pytest |
6 | 7 | import requests |
7 | 8 |
|
8 | | -import pandas.util.testing as tm |
| 9 | +from pandas_datareader.compat import assert_raises_regex |
9 | 10 | from pandas_datareader.wb import (search, download, get_countries, |
10 | 11 | get_indicators, WorldBankReader) |
11 | | -from pandas_datareader.compat import assert_raises_regex |
12 | 12 |
|
13 | 13 |
|
14 | 14 | class TestWB(object): |
@@ -214,3 +214,64 @@ def test_wdi_get_indicators(self): |
214 | 214 | # assert_index_equal doesn't exists |
215 | 215 | assert result.columns.equals(exp_col) |
216 | 216 | assert len(result) > 10000 |
| 217 | + |
| 218 | + def test_wdi_download_monthly(self): |
| 219 | + expected = {'COPPER': {('World', '2012M01'): 8040.47, |
| 220 | + ('World', '2011M12'): 7565.48, |
| 221 | + ('World', '2011M11'): 7581.02, |
| 222 | + ('World', '2011M10'): 7394.19, |
| 223 | + ('World', '2011M09'): 8300.14, |
| 224 | + ('World', '2011M08'): 9000.76, |
| 225 | + ('World', '2011M07'): 9650.46, |
| 226 | + ('World', '2011M06'): 9066.85, |
| 227 | + ('World', '2011M05'): 8959.90, |
| 228 | + ('World', '2011M04'): 9492.79, |
| 229 | + ('World', '2011M03'): 9503.36, |
| 230 | + ('World', '2011M02'): 9867.60, |
| 231 | + ('World', '2011M01'): 9555.70}} |
| 232 | + expected = pd.DataFrame(expected) |
| 233 | + # Round, to ignore revisions to data. |
| 234 | + expected = np.round(expected, decimals=-3) |
| 235 | + expected = expected.sort_index() |
| 236 | + cntry_codes = 'ALL' |
| 237 | + inds = 'COPPER' |
| 238 | + result = download(country=cntry_codes, indicator=inds, |
| 239 | + start=2011, end=2012, freq='M', errors='ignore') |
| 240 | + result = result.sort_index() |
| 241 | + result = np.round(result, decimals=-3) |
| 242 | + |
| 243 | + expected.index.names = ['country', 'year'] |
| 244 | + tm.assert_frame_equal(result, expected) |
| 245 | + |
| 246 | + result = WorldBankReader(inds, countries=cntry_codes, start=2011, |
| 247 | + end=2012, freq='M', errors='ignore').read() |
| 248 | + result = result.sort_index() |
| 249 | + result = np.round(result, decimals=-3) |
| 250 | + tm.assert_frame_equal(result, expected) |
| 251 | + |
| 252 | + def test_wdi_download_quarterly(self): |
| 253 | + code = 'DT.DOD.PUBS.CD.US' |
| 254 | + expected = {code: {('Albania', '2012Q1'): 3240539817.18, |
| 255 | + ('Albania', '2011Q4'): 3213979715.15, |
| 256 | + ('Albania', '2011Q3'): 3187681048.95, |
| 257 | + ('Albania', '2011Q2'): 3248041513.86, |
| 258 | + ('Albania', '2011Q1'): 3137210567.92}} |
| 259 | + expected = pd.DataFrame(expected) |
| 260 | + # Round, to ignore revisions to data. |
| 261 | + expected = np.round(expected, decimals=-3) |
| 262 | + expected = expected.sort_index() |
| 263 | + cntry_codes = 'ALB' |
| 264 | + inds = 'DT.DOD.PUBS.CD.US' |
| 265 | + result = download(country=cntry_codes, indicator=inds, |
| 266 | + start=2011, end=2012, freq='Q', errors='ignore') |
| 267 | + result = result.sort_index() |
| 268 | + result = np.round(result, decimals=-3) |
| 269 | + |
| 270 | + expected.index.names = ['country', 'year'] |
| 271 | + tm.assert_frame_equal(result, expected) |
| 272 | + |
| 273 | + result = WorldBankReader(inds, countries=cntry_codes, start=2011, |
| 274 | + end=2012, freq='Q', errors='ignore').read() |
| 275 | + result = result.sort_index() |
| 276 | + result = np.round(result, decimals=-1) |
| 277 | + tm.assert_frame_equal(result, expected) |
0 commit comments