For automating requests to the UN comtrade trade database, which is a repository of official international trade statistics and relevant analytical tables.
To install, clone the repo and run setup.py
:
git clone https://github.com/moody-marlin/un_comtrader.git
cd un_comtrader
python setup.py
OR use pip
:
pip install git+https://github.com/moody-marlin/un_comtrader.git
To import the module, open Python and run:
>>> from uncomtrader import ComtradeRequest
There are currently three ways to initialize a UN Comtrade Request:
This method starts from an empty request and sequentially adds the necessary request attributes.
>>> req = ComtradeRequest()
>>> req.type = "C"
>>> req.freq = "A"
>>> req.time_period = [2014,2015,2016]
>>> req.reporting area = "all"
>>> req.partner_area = 36
>>> req.hs = [44,4401,4402]
This method sets all of the attributes upon initialization.
>>> req = ComtradeRequest(type = "C", freq = "A", time_period = [2014,2015,2016],\
... reporting_area = "all", partner_area = 36, hs = [44,4401,4402])
This method intializes a request from a json file.
>>> file_loc = "path/to/json/file.json" # example .json is in data/ directory in repo
>>> req = ComtradeRequest.from_file(file_loc)
There are currently two methods to pull data from UN Comtrade.
This method pulls data and returns it as a pandas dataframe.
>>> df = req.pull_data()
>>> df.head()
This data can then be saved with:
>>> df.to_csv("path/to/where/you/want/tosave.csv")
This method pulls data and saves it as a csv or json.
>>> req.pull_data(save = "path/to/where/you/want/tosave.csv")
Note that the data is still available by calling
>>> df = req.data
If your single data request violates the usage limits, a MultiRequest
is necessary; the syntax remains the same, but MultiRequest
is capable of breaking your requests into smaller allowable requests; there are two ways to initialize a MultiRequest, but the recommended way is from a .json
file:
>>> from uncomtrader import MultiRequest
>>> req = MultiRequest.from_file("data/multirequest.json") #found in the repo
>>> print(req)
Currently storing 2 Comtrade Requests with URLs:
http://comtrade.un.org/api/get?fmt=csv&p=36&r=all&ps=2011,2012,2013,2014,2015&px=HS&cc=44,4401&freq=A&type=C
http://comtrade.un.org/api/get?fmt=csv&p=36&r=all&ps=2016&px=HS&cc=44,4401&freq=A&type=C
>>> data = req.pull_data()
>>> help(req)
OR
>>> help(ComtradeRequest)