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
  • Uses a cached internal client for efficiency
  • Zero configuration required

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.4.0.tar.gz (155.8 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.4.0-py3-none-any.whl (155.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ustrade-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e90dd5487690acfdd140900afdd855808f575ff4caed5ead118b1bcae4e313ac
MD5 45a9e3cf24d7306e678208935b7df8d7
BLAKE2b-256 20095fcfcbf0a564158ef2977b9d4886fe9a69e046889195d5fa4aef779f39e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ustrade-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 155.7 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.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95874ee174f4bdee59978c634917043118f23b6bc08a264064540716e9db0097
MD5 a54f35d0f6fd373b87c87ac060d43587
BLAKE2b-256 067e4e9fb1b0c715bad47ce632f6c2b462c23c4f151b564247c3c7335f0c5d4d

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