Repository: mwouts/world_trade_data

Issue:
IndexError with wits.get_tariff_reported
Hello,

I'm getting an IndexError with the following line(s) of code:
`wits.get_tariff_reported(reporter='840',partner='124',product='all',year='2012')` OR
`wits.get_tariff_reported(reporter='840',partner='124',product='all',year='2012', name_or_id = 'id')` 

The output says, "IndexError: list index out of range."

Could you assist with this issue?

Thanks in advance - Eric


Discussion:

Patch:
diff --git a/.travis.yml b/.travis.yml
index 3ac5038..5fc3b45 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,12 +1,11 @@
-dist: xenial   # required for Python >= 3.7
+dist: focal   # required for Python >= 3.10
 language: python
 python:
   - "3.6"
   - "3.7"
-  - "2.7"
-  - "3.4"
-  - "3.5"
-  - "3.8-dev"
+  - "3.8"
+  - "3.9"
+  - "3.10"
 install:
   # command to install dependencies
   - pip install -r requirements-dev.txt
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b9a414a..f46993a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+0.1.1 (2022-08-15)
+==================
+
+**Fixed**
+- Fixed an IndexError when calling `wits.get_tariff_reported` ([#3](https://github.com/mwouts/world_trade_data/issues/3))
+
+**Changed**
+- Versions of Python supported are 3.6 to 3.10.
+
+
 0.1.0 (2019-11-25)
 ==================
 
diff --git a/README.md b/README.md
index 80b6bcc..90188d8 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,15 @@ The nomenclature, and data availability, are accessible with `get_nomenclatures(
 
 Indicators are available with `get_indicator`. Tariff rates can be loaded with `get_tariff_reported` and `get_tariff_estimated`.
 
+## Working with codes rather than with category names
+
+The three functions above accept a `name_or_id` argument that defaults to `'name'`. Use `name_or_id='id'` to 
+get codes rather than full description for products and countries:
+
+```python
+wits.get_indicator('MPRT-TRD-VL', reporter='usa', year='2017', name_or_id='id')
+```
+
 ## Sample use case
 
 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')
 - The WITS data can be accessed in R with the [tradestatistics](https://tradestatistics.io/) library.
 - An alternative way to access the WITS data is to use [pandasdmx](https://pandasdmx.readthedocs.io/).
 
-<script async defer src="https://buttons.github.io/buttons.js"></script>
\ No newline at end of file
+<script async defer src="https://buttons.github.io/buttons.js"></script>
diff --git a/setup.py b/setup.py
index fda326c..8ffdabb 100644
--- a/setup.py
+++ b/setup.py
@@ -34,10 +34,9 @@ setup(
                  'Intended Audience :: Education',
                  'Intended Audience :: Science/Research',
                  'Programming Language :: Python',
-                 'Programming Language :: Python :: 2.7',
-                 'Programming Language :: Python :: 3.4',
-                 'Programming Language :: Python :: 3.5',
                  'Programming Language :: Python :: 3.6',
                  'Programming Language :: Python :: 3.7',
-                 'Programming Language :: Python :: 3.8']
+                 'Programming Language :: Python :: 3.8',
+                 'Programming Language :: Python :: 3.9',
+                 'Programming Language :: Python :: 3.10']
 )
diff --git a/world_trade_data/data.py b/world_trade_data/data.py
index 43a324d..aa6e849 100644
--- a/world_trade_data/data.py
+++ b/world_trade_data/data.py
@@ -1,6 +1,8 @@
 """WITS Data: indicators and tariffs"""
 
 import logging
+import warnings
+
 import requests
 import pandas as pd
 import world_trade_data.defaults
@@ -33,7 +35,7 @@ def get_tariff_reported(reporter,
                         year=world_trade_data.defaults.DEFAULT_YEAR,
                         name_or_id='name'):
     """Tariffs (reported)"""
-    return _get_data(reporter, partner, product, year,
+    return _get_data(reporter, partner, product, year, is_tariff=True,
                      datatype='reported', datasource='trn', name_or_id=name_or_id)
 
 
@@ -43,7 +45,7 @@ def get_tariff_estimated(reporter,
                          year=world_trade_data.defaults.DEFAULT_YEAR,
                          name_or_id='name'):
     """Tariffs (estimated)"""
-    return _get_data(reporter, partner, product, year,
+    return _get_data(reporter, partner, product, year,  is_tariff=True,
                      datatype='aveestimated', datasource='trn', name_or_id=name_or_id)
 
 
@@ -59,7 +61,7 @@ def get_indicator(indicator,
                      indicator=indicator, datasource=datasource, name_or_id=name_or_id)
 
 
-def _get_data(reporter, partner, product, year, datasource, name_or_id, **kwargs):
+def _get_data(reporter, partner, product, year, datasource, name_or_id, is_tariff=False, **kwargs):
     args = {'reporter': reporter,
             'partner': partner,
             'product': product,
@@ -83,16 +85,22 @@ def _get_data(reporter, partner, product, year, datasource, name_or_id, **kwargs
                             .format('/'.join(list_args)))
     response.raise_for_status()
     data = response.json()
-    return _wits_data_to_df(data, name_or_id=name_or_id)
+    df = _wits_data_to_df(data, name_or_id=name_or_id, is_tariff=is_tariff)
+    if is_tariff and not len(df):
+        warnings.warn("""Did you know? The reporter-partner combination only yields results
+ if the two countries have a preferential trade agreement (PTA).
+ Otherwise, all other tariffs to all non-PTA countries
+ are found if one enters "000" in partner.""")
+    return df
 
 
-def _wits_data_to_df(data, value_name='Value', name_or_id='id'):
+def _wits_data_to_df(data, value_name='Value', is_tariff=False, name_or_id='id'):
     observation = data['structure']['attributes']['observation']
     levels = data['structure']['dimensions']['series']
     obs_levels = data['structure']['dimensions']['observation']
     series = data['dataSets'][0]['series']
 
-    index_names = [l['name'] for l in levels] + [l['name'] for l in obs_levels]
+    index_names = [level['name'] for level in levels] + [obs_level['name'] for obs_level in obs_levels]
     column_names = [value_name] + [o['name'] for o in observation]
 
     all_observations = {value_name: []}
@@ -106,7 +114,7 @@ def _wits_data_to_df(data, value_name='Value', name_or_id='id'):
 
         # When loading tariffs, product is at depth 3, but levels say it's at depth 4
         # - So we invert the two levels
-        if value_name == 'Rate':
+        if is_tariff:
             loc[2], loc[3] = loc[3], loc[2]
 
         observations = series[i]['observations']
diff --git a/world_trade_data/version.py b/world_trade_data/version.py
index 973e52c..7076bb0 100644
--- a/world_trade_data/version.py
+++ b/world_trade_data/version.py
@@ -1,3 +1,3 @@
 """version number"""
 
-__version__ = '0.1.0'
+__version__ = '0.1.1'


Test patch:
diff --git a/tests/test_data.py b/tests/test_data.py
index b416864..c14f499 100644
--- a/tests/test_data.py
+++ b/tests/test_data.py
@@ -33,18 +33,30 @@ def test_get_tariff_reported():
     assert df.Value.dtype == np.float64
 
 
+def test_get_tariff_reported_issue_3():
+    df = get_tariff_reported(reporter='840', partner='124', product='all', year='2012')
+    assert df.Value.dtype == np.float64
+    assert len(df.index) > 100
+
+
 def test_get_tariff_estimated():
     df = get_tariff_estimated(reporter='840', partner='000', product='970600')
     assert len(df.index) == 1
     assert df.Value.dtype == np.float64
 
 
+def test_get_tariff_estimated_issue_3():
+    df = get_tariff_estimated(reporter='840', partner='124', product='all', year='2012')
+    assert df.Value.dtype == np.float64
+    assert len(df.index) > 100
+
+
 def test_tariff_data_to_df():
     current_path = os.path.dirname(__file__)
     sample_file = os.path.join(current_path, 'data', 'sample_tariff_data.json')
     with open(sample_file) as fp:
         data = json.load(fp)
-    df = _wits_data_to_df(data, 'Rate')
+    df = _wits_data_to_df(data, is_tariff=True)
     assert len(df.index) > 1
     assert len(df.columns) > 1