Skip to content

For automating requests to the UN comtrade trade database.

Notifications You must be signed in to change notification settings

cicdw/un_comtrader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

uncomtrader

For automating requests to the UN comtrade trade database, which is a repository of official international trade statistics and relevant analytical tables.

Installation

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

Importing

To import the module, open Python and run:

>>> from uncomtrader import ComtradeRequest

Initialize a Request

There are currently three ways to initialize a UN Comtrade Request:

Method 1

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]

Method 2

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])

Method 3

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)

Pull data

There are currently two methods to pull data from UN Comtrade.

Method 1

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")

Method 2

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

Large Requests

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

>>> help(req)

OR

>>> help(ComtradeRequest)

About

For automating requests to the UN comtrade trade database.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages