Skip to content

Commit 5f2a872

Browse files
authored
Varsion 0.1.1
**BugFixes** - Fixed `simplify_index` when the data is a scalar - Non-WDI indicators can now be loaded (#4) - Package also works under Python 2.7 (#1)
2 parents 2ae77ed + 26eae3f commit 5f2a872

15 files changed

+164
-49
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ language: python
33
python:
44
- "3.6"
55
- "3.7"
6+
- "2.7"
7+
- "3.4"
8+
- "3.5"
69
install:
710
# command to install dependencies
811
- pip install -r requirements-dev.txt

HISTORY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Release History
2+
===============
3+
4+
0.1.1 (2019-04-09)
5+
------------------
6+
7+
**BugFixes**
8+
9+
- Fixed `simplify_index` when the data is a scalar
10+
- Non-WDI indicators can now be loaded (#4)
11+
- Package also works under Python 2.7 (#1)
12+
13+
0.1.0 (2019-04-06)
14+
------------------
15+
16+
Initial release

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Go to our Binder and run either this [README](https://mybinder.org/v2/gh/mwouts/
100100

101101
## The World Bank
102102

103-
The [World Bank](https://www.worldbank.org/) Data has a [Data Catalog](https://datacatalog.worldbank.org/), and an interactive [data explorer](https://data.worldbank.org/indicator/sp.pop.totl).
103+
The [World Bank](https://www.worldbank.org/) has a [Data Catalog](https://datacatalog.worldbank.org/), and an interactive [data explorer](https://data.worldbank.org/indicator/sp.pop.totl).
104104

105105
Third party applications that allow to access the data from various languages are listed [here](https://data.worldbank.org/products/third-party-apps).
106106

@@ -110,9 +110,10 @@ The World Bank data is also available in Google's [Data Explorer](https://data.w
110110

111111
## Python
112112

113-
Alternatively to `world_bank_data`, Python users may find useful the following two packages:
113+
Alternatively to `world_bank_data`, Python users may find useful the following packages:
114114
- [`wbpy`](https://github.com/mattduck/wbpy/blob/master/README.rst), nicely documented but last released in 2013.
115115
- [`wbdata`](https://github.com/oliversherouse/wbdata/blob/master/README.rst), which works well.
116+
- [`pandas_datareader`](https://pandas-datareader.readthedocs.io/en/latest/readers/world-bank.html)
116117

117118
The reason for which I wrote `world_bank_data` is mostly speed, e.g. I wanted to use the lastest version of the World Bank API (v2) and benefit from significant speed improvements. Reimplementing the API also gave me a finer control on the mapping of options.
118119

@@ -128,7 +129,7 @@ See also the [Introduction to the wbstats R-package](https://cran.r-project.org/
128129

129130
## Country and indicator description in non-English languages
130131

131-
The World Bank describes their sources and indicators in a other languages than English. Use either the `language` argument in each of `get_countries`, `get_indicators`, etc, or change the default globally:
132+
The World Bank describes their sources and indicators in other languages than English. Use either the `language` argument in each of `get_countries`, `get_indicators`, etc, or change the default globally:
132133

133134
```python
134135
wb.options.language = 'vi'

examples/A sunburst plot of the world population.ipynb

Lines changed: 22 additions & 9 deletions
Large diffs are not rendered by default.

examples/A sunburst plot of the world population.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@
2020

2121
# +
2222
import pandas as pd
23-
import urllib
2423
import mock
2524
import plotly.offline as offline
2625
import world_bank_data as wb
2726

27+
try:
28+
# Python 3.6
29+
from urllib.request import urlopen
30+
except ImportError:
31+
# Python 2.7
32+
from urllib import urlopen
33+
2834
# Only show head and tail of dataframes
2935
pd.set_option('display.max_rows', 6)
3036

3137

3238
# Plotly.js in version 1.46.1
3339
def get_latest_plotlyjs(url='https://cdn.plot.ly/plotly-1.46.1.min.js'):
34-
return urllib.request.urlopen(url).read().decode('utf-8')
40+
return urlopen(url).read().decode('utf-8')
3541

3642

3743
with mock.patch('plotly.offline.offline.get_plotlyjs', get_latest_plotlyjs):
@@ -80,7 +86,9 @@ def get_latest_plotlyjs(url='https://cdn.plot.ly/plotly-1.46.1.min.js'):
8086

8187
# And now we can plot the World Population
8288
offline.iplot(dict(
83-
data=[dict(type='sunburst', **all_levels, hoverinfo='text')],
89+
data=[dict(type='sunburst', hoverinfo='text', **all_levels)],
8490
layout=dict(title='World Population (World Bank, 2017)<br>Click on a region to zoom',
8591
width=800, height=800)),
8692
validate=False)
93+
94+

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
'Intended Audience :: Education',
3434
'Intended Audience :: Science/Research',
3535
'Programming Language :: Python',
36+
'Programming Language :: Python :: 2.7',
37+
'Programming Language :: Python :: 3.4',
38+
'Programming Language :: Python :: 3.5',
3639
'Programming Language :: Python :: 3.6',
3740
'Programming Language :: Python :: 3.7')
3841
)

tests/test_country.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_country_language():
2525

2626
def test_two_countries():
2727
cnt = get_countries(['FRA', 'ITA'])
28-
assert cnt.index.to_list() == ['FRA', 'ITA']
28+
assert set(cnt.index) == set(['FRA', 'ITA'])
2929
assert cnt.latitude.dtype == float
3030
assert_numeric_or_string(cnt)
3131

tests/test_indicator.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import numbers
23
from world_bank_data import get_indicators, get_series
34
from .tools import assert_numeric_or_string
45

@@ -26,6 +27,15 @@ def test_indicators_topic():
2627
assert_numeric_or_string(idx)
2728

2829

30+
def test_indicators_source():
31+
idx = get_indicators(source=11)
32+
assert len(idx.index) < 2000
33+
assert_numeric_or_string(idx)
34+
35+
with pytest.raises(ValueError):
36+
get_indicators(source=21)
37+
38+
2939
def test_indicator_most_recent_value():
3040
idx = get_series('SP.POP.TOTL', mrv=1)
3141
assert len(idx.index) > 200
@@ -36,6 +46,12 @@ def test_indicator_most_recent_value():
3646
assert_numeric_or_string(idx_mrv5)
3747

3848

49+
def test_non_wdi_indicator():
50+
idx = get_series('TX.VAL.MRCH.CD.WB', mrv=1)
51+
assert len(idx.index) > 50
52+
assert_numeric_or_string(idx)
53+
54+
3955
def test_indicator_use_id():
4056
idx = get_series('SP.POP.TOTL', mrv=1, id_or_value='id', simplify_index=True)
4157
assert len(idx.index) > 200
@@ -44,12 +60,29 @@ def test_indicator_use_id():
4460
assert idx.index.names == ['Country']
4561

4662

63+
def test_indicator_simplify_scalar():
64+
pop = get_series('SP.POP.TOTL', 'CHN', mrv=1, simplify_index=True)
65+
assert isinstance(pop, numbers.Number)
66+
67+
4768
def test_indicator_date():
4869
idx = get_series('SP.POP.TOTL', date='2010:2018')
4970
assert len(idx.index) > 200 * 8
5071
assert_numeric_or_string(idx)
5172

5273

74+
def test_indicator_values():
75+
idx = get_series('SP.POP.TOTL', date='2017', simplify_index=True).sort_values(ascending=False)
76+
assert len(idx.index) > 200
77+
assert idx.index.values[0] == 'World'
78+
assert idx.iloc[0] == 7530360149.0
79+
80+
idx = get_series('SP.POP.TOTL', date='2017', simplify_index=True, id_or_value='id').sort_values(ascending=False)
81+
assert len(idx.index) > 200
82+
assert idx.index.values[0] == 'WLD'
83+
assert idx.iloc[0] == 7530360149.0
84+
85+
5386
@pytest.mark.skip('jsonstat format not supported here')
5487
def test_indicator_monthly():
5588
idx = get_series('DPANUSSPB', country=['CHN', 'BRA'], date='2012M01:2012M08')

tests/test_language.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
# -*- coding: utf-8 -*-
2+
13
import re
24
import mock
35
from world_bank_data import search_countries
46

57

68
def test_language():
79
assert search_countries(re.compile('ES')).name[0] == 'Spain'
8-
assert search_countries(re.compile('ES'), language='es').name[0] == 'España'
10+
assert search_countries(re.compile('ES'), language='es').name[0] == u'España'
911

1012

1113
def test_language_through_options():
1214
assert search_countries(re.compile('ES')).name[0] == 'Spain'
1315
with mock.patch('world_bank_data.options.language', 'es'):
14-
assert search_countries(re.compile('ES')).name[0] == 'España'
16+
assert search_countries(re.compile('ES')).name[0] == u'España'

tests/test_others.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,43 @@
55
def test_lending_types():
66
df = get_lendingtypes()
77
assert df.index.names == ['id']
8-
assert df.columns.to_list() == ['iso2code', 'value']
8+
assert set(df.columns) == set(['iso2code', 'value'])
99
assert_numeric_or_string(df)
1010

1111

1212
def test_income_levels():
1313
df = get_incomelevels()
1414
assert df.index.names == ['id']
15-
assert df.columns.to_list() == ['iso2code', 'value']
15+
assert set(df.columns) == set(['iso2code', 'value'])
1616
assert_numeric_or_string(df)
1717

1818

1919
def test_topics():
2020
df = get_topics()
2121
assert df.index.names == ['id']
22-
assert df.columns.to_list() == ['value', 'sourceNote']
22+
assert set(df.columns) == set(['value', 'sourceNote'])
2323
assert_numeric_or_string(df)
2424

2525

2626
def test_sources():
2727
df = get_sources()
2828
assert df.index.names == ['id']
29-
assert df.columns.to_list() == ['lastupdated', 'name', 'code', 'description', 'url', 'dataavailability',
30-
'metadataavailability', 'concepts']
29+
assert set(df.columns) == set(['lastupdated', 'name', 'code', 'description', 'url', 'dataavailability',
30+
'metadataavailability', 'concepts'])
31+
assert_numeric_or_string(df)
32+
33+
34+
def test_sources_int():
35+
df = get_sources(11)
36+
assert df.index.names == ['id']
37+
assert set(df.columns) == set(['lastupdated', 'name', 'code', 'description', 'url', 'dataavailability',
38+
'metadataavailability', 'concepts'])
39+
assert_numeric_or_string(df)
40+
41+
42+
def test_sources_two_int():
43+
df = get_sources([11, 36])
44+
assert df.index.names == ['id']
45+
assert set(df.columns) == set(['lastupdated', 'name', 'code', 'description', 'url', 'dataavailability',
46+
'metadataavailability', 'concepts'])
3147
assert_numeric_or_string(df)

0 commit comments

Comments
 (0)