Skip to content

Commit 8bf57d4

Browse files
authored
New is_tariff argument in get_data (#4)
* New is_tariff argument in `get_data` * Fix flake8 errors * Test with Python 3.6 to 3.10 * Python 3.10 is only available on Ubuntu 20.04 (focal) * Add suggested tip * Document how to get product codes with `name_or_id='id'` * Update release date
1 parent efde800 commit 8bf57d4

File tree

7 files changed

+56
-19
lines changed

7 files changed

+56
-19
lines changed

.travis.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
dist: xenial # required for Python >= 3.7
1+
dist: focal # required for Python >= 3.10
22
language: python
33
python:
44
- "3.6"
55
- "3.7"
6-
- "2.7"
7-
- "3.4"
8-
- "3.5"
9-
- "3.8-dev"
6+
- "3.8"
7+
- "3.9"
8+
- "3.10"
109
install:
1110
# command to install dependencies
1211
- pip install -r requirements-dev.txt

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
0.1.1 (2022-08-15)
2+
==================
3+
4+
**Fixed**
5+
- Fixed an IndexError when calling `wits.get_tariff_reported` ([#3](https://github.com/mwouts/world_trade_data/issues/3))
6+
7+
**Changed**
8+
- Versions of Python supported are 3.6 to 3.10.
9+
10+
111
0.1.0 (2019-11-25)
212
==================
313

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ The nomenclature, and data availability, are accessible with `get_nomenclatures(
5858

5959
Indicators are available with `get_indicator`. Tariff rates can be loaded with `get_tariff_reported` and `get_tariff_estimated`.
6060

61+
## Working with codes rather than with category names
62+
63+
The three functions above accept a `name_or_id` argument that defaults to `'name'`. Use `name_or_id='id'` to
64+
get codes rather than full description for products and countries:
65+
66+
```python
67+
wits.get_indicator('MPRT-TRD-VL', reporter='usa', year='2017', name_or_id='id')
68+
```
69+
6170
## Sample use case
6271

6372
In the below we show how to collect and plot the Import and Export data for the USA in 2017.
@@ -135,4 +144,4 @@ fig.show(renderer='notebook_connected')
135144
- The WITS data can be accessed in R with the [tradestatistics](https://tradestatistics.io/) library.
136145
- An alternative way to access the WITS data is to use [pandasdmx](https://pandasdmx.readthedocs.io/).
137146

138-
<script async defer src="https://buttons.github.io/buttons.js"></script>
147+
<script async defer src="https://buttons.github.io/buttons.js"></script>

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@
3434
'Intended Audience :: Education',
3535
'Intended Audience :: Science/Research',
3636
'Programming Language :: Python',
37-
'Programming Language :: Python :: 2.7',
38-
'Programming Language :: Python :: 3.4',
39-
'Programming Language :: Python :: 3.5',
4037
'Programming Language :: Python :: 3.6',
4138
'Programming Language :: Python :: 3.7',
42-
'Programming Language :: Python :: 3.8']
39+
'Programming Language :: Python :: 3.8',
40+
'Programming Language :: Python :: 3.9',
41+
'Programming Language :: Python :: 3.10']
4342
)

tests/test_data.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,30 @@ def test_get_tariff_reported():
3333
assert df.Value.dtype == np.float64
3434

3535

36+
def test_get_tariff_reported_issue_3():
37+
df = get_tariff_reported(reporter='840', partner='124', product='all', year='2012')
38+
assert df.Value.dtype == np.float64
39+
assert len(df.index) > 100
40+
41+
3642
def test_get_tariff_estimated():
3743
df = get_tariff_estimated(reporter='840', partner='000', product='970600')
3844
assert len(df.index) == 1
3945
assert df.Value.dtype == np.float64
4046

4147

48+
def test_get_tariff_estimated_issue_3():
49+
df = get_tariff_estimated(reporter='840', partner='124', product='all', year='2012')
50+
assert df.Value.dtype == np.float64
51+
assert len(df.index) > 100
52+
53+
4254
def test_tariff_data_to_df():
4355
current_path = os.path.dirname(__file__)
4456
sample_file = os.path.join(current_path, 'data', 'sample_tariff_data.json')
4557
with open(sample_file) as fp:
4658
data = json.load(fp)
47-
df = _wits_data_to_df(data, 'Rate')
59+
df = _wits_data_to_df(data, is_tariff=True)
4860
assert len(df.index) > 1
4961
assert len(df.columns) > 1
5062

world_trade_data/data.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""WITS Data: indicators and tariffs"""
22

33
import logging
4+
import warnings
5+
46
import requests
57
import pandas as pd
68
import world_trade_data.defaults
@@ -33,7 +35,7 @@ def get_tariff_reported(reporter,
3335
year=world_trade_data.defaults.DEFAULT_YEAR,
3436
name_or_id='name'):
3537
"""Tariffs (reported)"""
36-
return _get_data(reporter, partner, product, year,
38+
return _get_data(reporter, partner, product, year, is_tariff=True,
3739
datatype='reported', datasource='trn', name_or_id=name_or_id)
3840

3941

@@ -43,7 +45,7 @@ def get_tariff_estimated(reporter,
4345
year=world_trade_data.defaults.DEFAULT_YEAR,
4446
name_or_id='name'):
4547
"""Tariffs (estimated)"""
46-
return _get_data(reporter, partner, product, year,
48+
return _get_data(reporter, partner, product, year, is_tariff=True,
4749
datatype='aveestimated', datasource='trn', name_or_id=name_or_id)
4850

4951

@@ -59,7 +61,7 @@ def get_indicator(indicator,
5961
indicator=indicator, datasource=datasource, name_or_id=name_or_id)
6062

6163

62-
def _get_data(reporter, partner, product, year, datasource, name_or_id, **kwargs):
64+
def _get_data(reporter, partner, product, year, datasource, name_or_id, is_tariff=False, **kwargs):
6365
args = {'reporter': reporter,
6466
'partner': partner,
6567
'product': product,
@@ -83,16 +85,22 @@ def _get_data(reporter, partner, product, year, datasource, name_or_id, **kwargs
8385
.format('/'.join(list_args)))
8486
response.raise_for_status()
8587
data = response.json()
86-
return _wits_data_to_df(data, name_or_id=name_or_id)
88+
df = _wits_data_to_df(data, name_or_id=name_or_id, is_tariff=is_tariff)
89+
if is_tariff and not len(df):
90+
warnings.warn("""Did you know? The reporter-partner combination only yields results
91+
if the two countries have a preferential trade agreement (PTA).
92+
Otherwise, all other tariffs to all non-PTA countries
93+
are found if one enters "000" in partner.""")
94+
return df
8795

8896

89-
def _wits_data_to_df(data, value_name='Value', name_or_id='id'):
97+
def _wits_data_to_df(data, value_name='Value', is_tariff=False, name_or_id='id'):
9098
observation = data['structure']['attributes']['observation']
9199
levels = data['structure']['dimensions']['series']
92100
obs_levels = data['structure']['dimensions']['observation']
93101
series = data['dataSets'][0]['series']
94102

95-
index_names = [l['name'] for l in levels] + [l['name'] for l in obs_levels]
103+
index_names = [level['name'] for level in levels] + [obs_level['name'] for obs_level in obs_levels]
96104
column_names = [value_name] + [o['name'] for o in observation]
97105

98106
all_observations = {value_name: []}
@@ -106,7 +114,7 @@ def _wits_data_to_df(data, value_name='Value', name_or_id='id'):
106114

107115
# When loading tariffs, product is at depth 3, but levels say it's at depth 4
108116
# - So we invert the two levels
109-
if value_name == 'Rate':
117+
if is_tariff:
110118
loc[2], loc[3] = loc[3], loc[2]
111119

112120
observations = series[i]['observations']

world_trade_data/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""version number"""
22

3-
__version__ = '0.1.0'
3+
__version__ = '0.1.1'

0 commit comments

Comments
 (0)