Skip to main content

World Integrated Trade Solution (WITS) API in Python

Project description

World Trade Statistics (WITS) API in Python

Build Status codecov.io Language grade: Python Pypi pyversions Jupyter Notebook GitHub.io Star

This package is an implementation of the World Integrated Trade Solution API. Use this package to explore the Trade and Tariff Data published by the World Bank.

This python package itself is licenced under the MIT License. Different Terms and Conditions apply to the WITS data itself, please read the Frequently Asked Questions on the WITS website.

Quick tutorial

Installation

Install or update the World Trade Data python package with

pip install world_trade_data --upgrade

Get the list of countries, products, indicators

See the outputs of the commands below on GitHub. Or even, open this README.md as a notebook and run it interactively on Binder!

import pandas as pd
import world_trade_data as wits
pd.set_option('display.max_rows', 6)
wits.get_countries()
wits.get_products()
wits.get_indicators()

All these methods accept a datasource argument, which can be any of

wits.DATASOURCES

The nomenclature, and data availability, are accessible with get_nomenclatures() and get_dataavailability().

Get the Trade or Tariff data

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

Sample use case

In the below we show how to collect and plot the Import and Export data for the USA in 2017.

To begin with, we request the values for the corresponding import and exports. Here, we use the default value for partner='wld', and the default value for product='all'.

usa_imports_2017 = wits.get_indicator('MPRT-TRD-VL', reporter='usa', year='2017')
usa_exports_2017 = wits.get_indicator('XPRT-TRD-VL', reporter='usa', year='2017')
usa_imports_2017

Now we remove the first levels of the index

usa_imports_2017 = usa_imports_2017.loc['Annual'].loc['United States'].loc['World']
usa_exports_2017 = usa_exports_2017.loc['Annual'].loc['United States'].loc['World']

Note that one line in the table gives the value for imports on all products:

usa_imports_2017.loc['All Products']

In order to avoid double counting, we only look at sectors:

products = wits.get_products()
sectors = products.loc[(products.grouptype=='Sector') & (products.index!='Total')].productdescription.values
sectors

and make sure that we reproduce well the total:

assert pd.np.isclose(usa_imports_2017.loc[sectors].Value.sum(), usa_imports_2017.loc['All Products'].Value)

Finally we represent the data using e.g. Plotly's Pie Charts

import plotly.graph_objects as go
from plotly.subplots import make_subplots

imports_musd = usa_imports_2017.loc[sectors].Value / 1e3
exports_musd = usa_exports_2017.loc[sectors].Value / 1e3

fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]])
fig.add_trace(go.Pie(labels=sectors, values=imports_musd, name="Imports"), 1, 1)
fig.add_trace(go.Pie(labels=sectors, values=exports_musd, name="Exports"), 1, 2)

fig.update_traces(hole=.4, 
                  scalegroup='usa',
                  textinfo='label',
                  hovertemplate = "%{label}<br>%{value:,.0f}M$<br>%{percent}")

fig.update_layout(
    title_text="Trade Statistics, USA, 2017",
    annotations=[dict(text='Imports<br>{:.3f}T$'.format(imports_musd.sum()/1e6),
                      x=0.17, y=0.5, font_size=16, showarrow=False),
                 dict(text='Exports<br>{:.3f}T$'.format(exports_musd.sum()/1e6),
                      x=0.83, y=0.5, font_size=16, showarrow=False)])
fig.show(renderer='notebook_connected')

References & Alternatives

  • The official WITS portal let you visualize and download trade and tariff data. And the API implemented in this package is documented here.
  • The WITS data can be accessed in R with the tradestatistics library.
  • An alternative way to access the WITS data is to use pandasdmx.
<script async defer src="https://buttons.github.io/buttons.js"></script>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

world_trade_data-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

File details

Details for the file world_trade_data-0.1.0.tar.gz.

File metadata

  • Download URL: world_trade_data-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for world_trade_data-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1d74fb4b45b985e2a38cb091960ceb5fb03e82650ff6c429395c5deb5ee07d43
MD5 76559cb0ed2435ef86e621a5677ae0b5
BLAKE2b-256 65068dd602728a33cb95a07849fb11adaa8127692552a2271402a522b46ef67d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page