Skip to main content

Python client for the U.S. Census Bureau International Trade API

Project description

ustrade

A lightweight and intuitive Python client for the U.S. Census Bureau International Trade API.
Allows to fetch imports and exports between the US and commercial partners - based on Harmonized System codes (HS)


Features

  • Simple API: ust.get_imports(), ust.get_exports()
  • Automatic normalization of country inputs:
    • "France"
    • "FR" (ISO2)
    • "4279" (Census code)
  • HS codes lookup + product descriptions
  • Standardized DataFrame output with clean column names

For interactive exploration and non-Python usage, a Streamlit dashboard is available for this library at https://ustrade.streamlit.app.

Source code : https://github.com/fantinsib/ustrade_dashboard


Installation

PyPI Install

Install with pip :

pip install ustrade

Github

Clone this repository and install via pip in editable mode:

git clone https://github.com/fantinsib/ustrade.git
cd ustrade
pip install -e .

Quick Example

import ustrade as ust

# Example: Mexican imports of fruits and nuts between January 2010 and January 2025
df = ust.get_imports_on_period("Mexico", "08", "2010-01", "2025-01")
print(df)

Full API Reference

Fetching Data

get_imports(country, product, date)

Fetch monthly import data for a given country and HS code.

Parameters:

  • country — country name (France), ISO2 (FR), Census code (4279) or Country instance, or a list of the previous
  • product — HS code as string (e.g. 2701) or list of string
  • dateYYYY-MM format (e.g. 2020-01)

Example:

ust.get_imports("FR", "10", "2025-01")
ust.get_imports(["France", "GB"], ["12", "13"], "2018-03")

get_exports(country, product, date)

Fetch monthly export data for a given country and HS code.

Example:

ust.get_exports("GB", "73", "2019-01")
ust.get_exports(["France", "GB"], ["08", "09"], "2018-03")

get_imports_on_period(country, product, start, end)

Fetch a DataFrame containing monthly imports for a given country and HS code

Parameters:

  • country — country name (France), ISO2 (FR), Census code (4279) or Country instance, or list of the previous
  • product — HS code as string (e.g. 2701) or list of string
  • start and endYYYY-MM format (e.g. 2020-01)

Example:

ust.get_imports_on_period("Mexico", "27", "2010-01", "2025-01")
ust.get_imports_on_period(["France", "DE", "GB"], ["09", "08", "07"], "2016-01", "2018-01")

get_exports_on_period(country, product, start, end)

Fetch a DataFrame containing monthly exports for a given country and HS code

Example:

ust.get_exports_on_period("France", "10", "2020-01", "2023-01")

Exploring Codes

HS Codes follow a tree hierarchy.

get_desc_from_code(hs)

Return the description associated with an HS code.

Example:

ust.get_desc_from_code("2701")

get_children_codes(code, return_names)

Return a dictionnary of the codes and descriptions attached to the parent code if return_names is True, and a list of the code if return_names if False.

Example:

ust.get_children_codes("10")

get_product(hs)

Return the HSCode instance associated with the hs code specified.

Example:

ust.get_product("10")

Exploring countries

get_country_by_name(name)

Look up a country by its full name (case-insensitive).

Example:

ust.get_country_by_name("France")

get_country_by_code(code)

Look up a country using its U.S. Census numeric code.

Example:

ust.get_country_by_code("4279")

get_country_by_iso2(iso)

Look up a country by ISO2 code.

Example:

ust.get_country_by_iso2("FR")

🧩 Notes

  • All data retrieval functions return a pandas DataFrame unless otherwise noted.
  • Column names are automatically standardized (see schema section).
  • This library is still in <1.0.0 version and can change. Contributions are always welcome !

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

ustrade-0.5.0.tar.gz (158.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ustrade-0.5.0-py3-none-any.whl (156.9 kB view details)

Uploaded Python 3

File details

Details for the file ustrade-0.5.0.tar.gz.

File metadata

  • Download URL: ustrade-0.5.0.tar.gz
  • Upload date:
  • Size: 158.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for ustrade-0.5.0.tar.gz
Algorithm Hash digest
SHA256 48b49fde33b015bf93ac3b4bb36b78baa2d03577bc5c10909fac8c20aa6fc566
MD5 e9bc4b384b8e84052ceb27db873c6e26
BLAKE2b-256 60a3bd1a03f31062a0689542b5d7d37afd888987a1e473cc6f78248ed1d9381f

See more details on using hashes here.

File details

Details for the file ustrade-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: ustrade-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 156.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for ustrade-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0172922c3853a4e9ee219c9b35b3ea0ab20d35c5665cb60ee863f4c884f92383
MD5 93ddaad065e32762751d3cce43ac0c99
BLAKE2b-256 c2b036809ecf67e2077d49e93500288eb583d5724042c0c641c1fc8c4d7cc60f

See more details on using hashes here.

Supported by

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