comexpy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- comexpy-0.1.0/.github/workflows/docs.yml +25 -0
- comexpy-0.1.0/.github/workflows/publish.yml +52 -0
- comexpy-0.1.0/.gitignore +36 -0
- comexpy-0.1.0/LICENSE +21 -0
- comexpy-0.1.0/PKG-INFO +189 -0
- comexpy-0.1.0/README.md +134 -0
- comexpy-0.1.0/docs/getting-started.md +69 -0
- comexpy-0.1.0/docs/guide.md +110 -0
- comexpy-0.1.0/docs/index.md +45 -0
- comexpy-0.1.0/docs/reference.md +55 -0
- comexpy-0.1.0/mkdocs.yml +66 -0
- comexpy-0.1.0/pyproject.toml +56 -0
- comexpy-0.1.0/src/comexpy/__init__.py +102 -0
- comexpy-0.1.0/src/comexpy/_client.py +202 -0
- comexpy-0.1.0/src/comexpy/_format.py +275 -0
- comexpy-0.1.0/src/comexpy/_msg.py +47 -0
- comexpy-0.1.0/src/comexpy/historical.py +118 -0
- comexpy-0.1.0/src/comexpy/query.py +188 -0
- comexpy-0.1.0/src/comexpy/query_city.py +112 -0
- comexpy-0.1.0/src/comexpy/tables.py +223 -0
- comexpy-0.1.0/src/comexpy/tables_classifications.py +136 -0
- comexpy-0.1.0/src/comexpy/tables_products.py +146 -0
- comexpy-0.1.0/tests/test_client.py +101 -0
- comexpy-0.1.0/tests/test_format.py +153 -0
- comexpy-0.1.0/tests/test_queries.py +156 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: docs
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: write
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build-deploy:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.x"
|
|
20
|
+
|
|
21
|
+
- name: Install package and docs dependencies
|
|
22
|
+
run: pip install -e ".[docs]"
|
|
23
|
+
|
|
24
|
+
- name: Build and deploy to gh-pages
|
|
25
|
+
run: mkdocs gh-deploy --force --no-history
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
name: publish
|
|
2
|
+
|
|
3
|
+
# Publishes to PyPI on a GitHub Release (or manual dispatch) using PyPI
|
|
4
|
+
# Trusted Publishing (OIDC) — no API token stored in the repository.
|
|
5
|
+
# One-time setup on PyPI: add a "pending publisher" for project `comexpy`
|
|
6
|
+
# pointing at owner `StrategicProjects`, repo `comexpy`, workflow `publish.yml`.
|
|
7
|
+
on:
|
|
8
|
+
release:
|
|
9
|
+
types: [published]
|
|
10
|
+
workflow_dispatch:
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: "3.x"
|
|
21
|
+
|
|
22
|
+
- name: Build sdist and wheel
|
|
23
|
+
run: |
|
|
24
|
+
python -m pip install --upgrade build
|
|
25
|
+
python -m build
|
|
26
|
+
|
|
27
|
+
- name: Check artifacts
|
|
28
|
+
run: |
|
|
29
|
+
python -m pip install --upgrade twine
|
|
30
|
+
twine check dist/*
|
|
31
|
+
|
|
32
|
+
- uses: actions/upload-artifact@v4
|
|
33
|
+
with:
|
|
34
|
+
name: dist
|
|
35
|
+
path: dist/
|
|
36
|
+
|
|
37
|
+
publish:
|
|
38
|
+
needs: build
|
|
39
|
+
runs-on: ubuntu-latest
|
|
40
|
+
environment:
|
|
41
|
+
name: pypi
|
|
42
|
+
url: https://pypi.org/project/comexpy/
|
|
43
|
+
permissions:
|
|
44
|
+
id-token: write # required for Trusted Publishing (OIDC)
|
|
45
|
+
steps:
|
|
46
|
+
- uses: actions/download-artifact@v4
|
|
47
|
+
with:
|
|
48
|
+
name: dist
|
|
49
|
+
path: dist/
|
|
50
|
+
|
|
51
|
+
- name: Publish to PyPI
|
|
52
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
comexpy-0.1.0/.gitignore
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# Byte-compiled / optimized
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Distribution / packaging
|
|
7
|
+
build/
|
|
8
|
+
dist/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
.eggs/
|
|
11
|
+
wheels/
|
|
12
|
+
|
|
13
|
+
# Virtual environments
|
|
14
|
+
.venv/
|
|
15
|
+
venv/
|
|
16
|
+
env/
|
|
17
|
+
|
|
18
|
+
# MkDocs build output
|
|
19
|
+
site/
|
|
20
|
+
|
|
21
|
+
# Test / coverage
|
|
22
|
+
.pytest_cache/
|
|
23
|
+
.coverage
|
|
24
|
+
htmlcov/
|
|
25
|
+
.ruff_cache/
|
|
26
|
+
.mypy_cache/
|
|
27
|
+
|
|
28
|
+
# Editors / OS
|
|
29
|
+
.DS_Store
|
|
30
|
+
.idea/
|
|
31
|
+
.vscode/
|
|
32
|
+
|
|
33
|
+
# Claude Code: local guidance and memory (not for the repo)
|
|
34
|
+
CLAUDE.md
|
|
35
|
+
CLAUDE.local.md
|
|
36
|
+
.claude/
|
comexpy-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 comexpy authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
comexpy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: comexpy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Access the Brazilian Foreign Trade Statistics API (ComexStat) from Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/StrategicProjects/comexpy
|
|
6
|
+
Project-URL: Repository, https://github.com/StrategicProjects/comexpy
|
|
7
|
+
Project-URL: Issues, https://github.com/StrategicProjects/comexpy/issues
|
|
8
|
+
Project-URL: R package (comexr), https://github.com/StrategicProjects/comexr
|
|
9
|
+
Author-email: Andre Leite <leite@castlab.org>, Marcos Wasilew <marcos.wasilew@gmail.com>, Hugo Vasconcelos <hugo.vasconcelos@ufpe.br>, Carlos Amorin <carlos.agaf@ufpe.br>, Diogo Bezerra <diogo.bezerra@ufpe.br>
|
|
10
|
+
License: MIT License
|
|
11
|
+
|
|
12
|
+
Copyright (c) 2026 comexpy authors
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
15
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
16
|
+
in the Software without restriction, including without limitation the rights
|
|
17
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
18
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
19
|
+
furnished to do so, subject to the following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all
|
|
22
|
+
copies or substantial portions of the Software.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
25
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
26
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
27
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
28
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
29
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
30
|
+
SOFTWARE.
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Keywords: Brazil,ComexStat,MDIC,exports,foreign-trade,imports,open-data,pandas
|
|
33
|
+
Classifier: Development Status :: 4 - Beta
|
|
34
|
+
Classifier: Intended Audience :: Science/Research
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Operating System :: OS Independent
|
|
37
|
+
Classifier: Programming Language :: Python :: 3
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
42
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
43
|
+
Classifier: Topic :: Scientific/Engineering
|
|
44
|
+
Requires-Python: >=3.9
|
|
45
|
+
Requires-Dist: pandas>=1.3
|
|
46
|
+
Requires-Dist: requests>=2.25
|
|
47
|
+
Provides-Extra: dev
|
|
48
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
49
|
+
Requires-Dist: responses>=0.23; extra == 'dev'
|
|
50
|
+
Requires-Dist: ruff>=0.1; extra == 'dev'
|
|
51
|
+
Provides-Extra: docs
|
|
52
|
+
Requires-Dist: mkdocs-material>=9.5; extra == 'docs'
|
|
53
|
+
Requires-Dist: mkdocstrings[python]>=0.24; extra == 'docs'
|
|
54
|
+
Description-Content-Type: text/markdown
|
|
55
|
+
|
|
56
|
+
# comexpy
|
|
57
|
+
|
|
58
|
+
[](https://pypi.org/project/comexpy/)
|
|
59
|
+
[](https://pypi.org/project/comexpy/)
|
|
60
|
+
[](LICENSE)
|
|
61
|
+
|
|
62
|
+
**Access the Brazilian Foreign Trade Statistics API (ComexStat) from Python.**
|
|
63
|
+
|
|
64
|
+
`comexpy` is a pandas-friendly interface to the
|
|
65
|
+
[ComexStat API](https://api-comexstat.mdic.gov.br/docs#/) of the Brazilian
|
|
66
|
+
Ministry of Development, Industry, Trade and Services (MDIC). It provides
|
|
67
|
+
programmatic access to detailed Brazilian export and import data — every
|
|
68
|
+
query returns a `pandas.DataFrame`.
|
|
69
|
+
|
|
70
|
+
This is the Python port of the R package
|
|
71
|
+
[`comexr`](https://github.com/StrategicProjects/comexr); the public function
|
|
72
|
+
names mirror the R API so knowledge transfers directly between the two.
|
|
73
|
+
|
|
74
|
+
## Features
|
|
75
|
+
|
|
76
|
+
- **General trade data** (1997–present), **city-level** data, and
|
|
77
|
+
**historical** records (1989–1996)
|
|
78
|
+
- **Auxiliary tables**: countries, economic blocs, NCM/NBM/HS product codes,
|
|
79
|
+
CGCE/SITC/ISIC classifications, states, cities, transport modes, customs units
|
|
80
|
+
- **Friendly aliases** — pass `"hs4"`, `"transport_mode"`, `"cgce_n1"` and the
|
|
81
|
+
package translates them to the API's internal names
|
|
82
|
+
- **Multilingual** responses: Portuguese, English, Spanish
|
|
83
|
+
- **SSL auto-fallback** — handles ICP-Brasil certificate issues transparently
|
|
84
|
+
- **Configurable retry/timeout** for the API's aggressive rate limiting
|
|
85
|
+
|
|
86
|
+
## Installation
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
pip install comexpy
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Requires Python 3.9+, `requests` and `pandas`.
|
|
93
|
+
|
|
94
|
+
## Quick start
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import comexpy
|
|
98
|
+
|
|
99
|
+
# Exports by country in January 2024
|
|
100
|
+
exports = comexpy.comex_export(
|
|
101
|
+
start_period="2024-01",
|
|
102
|
+
end_period="2024-01",
|
|
103
|
+
details="country",
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Imports with CIF value, by country, for all of 2024
|
|
107
|
+
imports = comexpy.comex_import(
|
|
108
|
+
start_period="2024-01",
|
|
109
|
+
end_period="2024-12",
|
|
110
|
+
details="country",
|
|
111
|
+
metric_cif=True,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
# Exports to China (160), grouped by HS4
|
|
115
|
+
# (the package translates "hs4" to the API's `heading`)
|
|
116
|
+
soy = comexpy.comex_export(
|
|
117
|
+
start_period="2024-01",
|
|
118
|
+
end_period="2024-12",
|
|
119
|
+
details=["country", "hs4"],
|
|
120
|
+
filters={"country": 160},
|
|
121
|
+
)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Discover available options
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
comexpy.comex_details("general") # grouping fields available
|
|
128
|
+
comexpy.comex_filters("general") # filters available
|
|
129
|
+
comexpy.comex_metrics("general") # metrics available
|
|
130
|
+
|
|
131
|
+
# Look up country codes
|
|
132
|
+
countries = comexpy.comex_countries()
|
|
133
|
+
countries[countries["text"].str.contains("China", case=False)]
|
|
134
|
+
|
|
135
|
+
# Economic blocs in Portuguese
|
|
136
|
+
comexpy.comex_blocs(language="pt")
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## City-level and historical data
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
# Exports declared in Pernambuco (state 26) in 2023
|
|
143
|
+
comexpy.comex_query_city(
|
|
144
|
+
flow="export",
|
|
145
|
+
start_period="2023-01",
|
|
146
|
+
end_period="2023-12",
|
|
147
|
+
details=["country", "state"],
|
|
148
|
+
filters={"state": 26},
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
# Historical exports 1995–1996 by country (NBM classification)
|
|
152
|
+
comexpy.comex_historical(
|
|
153
|
+
flow="export",
|
|
154
|
+
start_period="1995-01",
|
|
155
|
+
end_period="1996-12",
|
|
156
|
+
details="country",
|
|
157
|
+
)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Rate limiting and timeouts
|
|
161
|
+
|
|
162
|
+
The ComexStat API frequently returns rate-limit errors (HTTP 429,
|
|
163
|
+
*"Você excedeu o limite de solicitações..."*) or times out. Adjust the
|
|
164
|
+
behaviour with `set_options`:
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
comexpy.set_options(retry_time=30) # wait 30s between retries
|
|
168
|
+
comexpy.set_options(max_tries=5, timeout_post=180)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Silence progress/success messages with `comexpy.set_verbose(False)`.
|
|
172
|
+
|
|
173
|
+
## Public API
|
|
174
|
+
|
|
175
|
+
| Function | Purpose |
|
|
176
|
+
|----------|---------|
|
|
177
|
+
| `comex_query` / `comex_export` / `comex_import` | General trade data (1997–present) |
|
|
178
|
+
| `comex_query_city` | City-level data |
|
|
179
|
+
| `comex_historical` | Historical data (1989–1996) |
|
|
180
|
+
| `comex_last_update` / `comex_available_years` | API metadata |
|
|
181
|
+
| `comex_filters` / `comex_filter_values` / `comex_details` / `comex_metrics` | Query option discovery |
|
|
182
|
+
| `comex_countries` / `comex_blocs` / `comex_states` / `comex_cities` / `comex_transport_modes` / `comex_customs_units` (+ `_detail`) | Geography tables |
|
|
183
|
+
| `comex_ncm` / `comex_nbm` / `comex_hs` (+ `_detail`) | Product nomenclatures |
|
|
184
|
+
| `comex_cgce` / `comex_sitc` / `comex_isic` | Economic classifications |
|
|
185
|
+
| `set_options` / `set_verbose` | Configuration |
|
|
186
|
+
|
|
187
|
+
## License
|
|
188
|
+
|
|
189
|
+
MIT — see [LICENSE](LICENSE). Developed by the comexpy authors.
|
comexpy-0.1.0/README.md
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# comexpy
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/comexpy/)
|
|
4
|
+
[](https://pypi.org/project/comexpy/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
**Access the Brazilian Foreign Trade Statistics API (ComexStat) from Python.**
|
|
8
|
+
|
|
9
|
+
`comexpy` is a pandas-friendly interface to the
|
|
10
|
+
[ComexStat API](https://api-comexstat.mdic.gov.br/docs#/) of the Brazilian
|
|
11
|
+
Ministry of Development, Industry, Trade and Services (MDIC). It provides
|
|
12
|
+
programmatic access to detailed Brazilian export and import data — every
|
|
13
|
+
query returns a `pandas.DataFrame`.
|
|
14
|
+
|
|
15
|
+
This is the Python port of the R package
|
|
16
|
+
[`comexr`](https://github.com/StrategicProjects/comexr); the public function
|
|
17
|
+
names mirror the R API so knowledge transfers directly between the two.
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- **General trade data** (1997–present), **city-level** data, and
|
|
22
|
+
**historical** records (1989–1996)
|
|
23
|
+
- **Auxiliary tables**: countries, economic blocs, NCM/NBM/HS product codes,
|
|
24
|
+
CGCE/SITC/ISIC classifications, states, cities, transport modes, customs units
|
|
25
|
+
- **Friendly aliases** — pass `"hs4"`, `"transport_mode"`, `"cgce_n1"` and the
|
|
26
|
+
package translates them to the API's internal names
|
|
27
|
+
- **Multilingual** responses: Portuguese, English, Spanish
|
|
28
|
+
- **SSL auto-fallback** — handles ICP-Brasil certificate issues transparently
|
|
29
|
+
- **Configurable retry/timeout** for the API's aggressive rate limiting
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install comexpy
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Requires Python 3.9+, `requests` and `pandas`.
|
|
38
|
+
|
|
39
|
+
## Quick start
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
import comexpy
|
|
43
|
+
|
|
44
|
+
# Exports by country in January 2024
|
|
45
|
+
exports = comexpy.comex_export(
|
|
46
|
+
start_period="2024-01",
|
|
47
|
+
end_period="2024-01",
|
|
48
|
+
details="country",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Imports with CIF value, by country, for all of 2024
|
|
52
|
+
imports = comexpy.comex_import(
|
|
53
|
+
start_period="2024-01",
|
|
54
|
+
end_period="2024-12",
|
|
55
|
+
details="country",
|
|
56
|
+
metric_cif=True,
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
# Exports to China (160), grouped by HS4
|
|
60
|
+
# (the package translates "hs4" to the API's `heading`)
|
|
61
|
+
soy = comexpy.comex_export(
|
|
62
|
+
start_period="2024-01",
|
|
63
|
+
end_period="2024-12",
|
|
64
|
+
details=["country", "hs4"],
|
|
65
|
+
filters={"country": 160},
|
|
66
|
+
)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Discover available options
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
comexpy.comex_details("general") # grouping fields available
|
|
73
|
+
comexpy.comex_filters("general") # filters available
|
|
74
|
+
comexpy.comex_metrics("general") # metrics available
|
|
75
|
+
|
|
76
|
+
# Look up country codes
|
|
77
|
+
countries = comexpy.comex_countries()
|
|
78
|
+
countries[countries["text"].str.contains("China", case=False)]
|
|
79
|
+
|
|
80
|
+
# Economic blocs in Portuguese
|
|
81
|
+
comexpy.comex_blocs(language="pt")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## City-level and historical data
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
# Exports declared in Pernambuco (state 26) in 2023
|
|
88
|
+
comexpy.comex_query_city(
|
|
89
|
+
flow="export",
|
|
90
|
+
start_period="2023-01",
|
|
91
|
+
end_period="2023-12",
|
|
92
|
+
details=["country", "state"],
|
|
93
|
+
filters={"state": 26},
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# Historical exports 1995–1996 by country (NBM classification)
|
|
97
|
+
comexpy.comex_historical(
|
|
98
|
+
flow="export",
|
|
99
|
+
start_period="1995-01",
|
|
100
|
+
end_period="1996-12",
|
|
101
|
+
details="country",
|
|
102
|
+
)
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Rate limiting and timeouts
|
|
106
|
+
|
|
107
|
+
The ComexStat API frequently returns rate-limit errors (HTTP 429,
|
|
108
|
+
*"Você excedeu o limite de solicitações..."*) or times out. Adjust the
|
|
109
|
+
behaviour with `set_options`:
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
comexpy.set_options(retry_time=30) # wait 30s between retries
|
|
113
|
+
comexpy.set_options(max_tries=5, timeout_post=180)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Silence progress/success messages with `comexpy.set_verbose(False)`.
|
|
117
|
+
|
|
118
|
+
## Public API
|
|
119
|
+
|
|
120
|
+
| Function | Purpose |
|
|
121
|
+
|----------|---------|
|
|
122
|
+
| `comex_query` / `comex_export` / `comex_import` | General trade data (1997–present) |
|
|
123
|
+
| `comex_query_city` | City-level data |
|
|
124
|
+
| `comex_historical` | Historical data (1989–1996) |
|
|
125
|
+
| `comex_last_update` / `comex_available_years` | API metadata |
|
|
126
|
+
| `comex_filters` / `comex_filter_values` / `comex_details` / `comex_metrics` | Query option discovery |
|
|
127
|
+
| `comex_countries` / `comex_blocs` / `comex_states` / `comex_cities` / `comex_transport_modes` / `comex_customs_units` (+ `_detail`) | Geography tables |
|
|
128
|
+
| `comex_ncm` / `comex_nbm` / `comex_hs` (+ `_detail`) | Product nomenclatures |
|
|
129
|
+
| `comex_cgce` / `comex_sitc` / `comex_isic` | Economic classifications |
|
|
130
|
+
| `set_options` / `set_verbose` | Configuration |
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT — see [LICENSE](LICENSE). Developed by the comexpy authors.
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# Getting started
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install comexpy
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Requires Python 3.9+, `requests` and `pandas`.
|
|
10
|
+
|
|
11
|
+
## Your first queries
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
import comexpy
|
|
15
|
+
|
|
16
|
+
# Exports by country in January 2024 (monthly detail off → one row per country)
|
|
17
|
+
exports = comexpy.comex_export(
|
|
18
|
+
start_period="2024-01",
|
|
19
|
+
end_period="2024-01",
|
|
20
|
+
details="country",
|
|
21
|
+
month_detail=False,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# Imports for all of 2024, by country, including the CIF value
|
|
25
|
+
imports = comexpy.comex_import(
|
|
26
|
+
start_period="2024-01",
|
|
27
|
+
end_period="2024-12",
|
|
28
|
+
details="country",
|
|
29
|
+
metric_cif=True,
|
|
30
|
+
)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Filtering and grouping
|
|
34
|
+
|
|
35
|
+
`details` controls the grouping; `filters` restricts the rows. Both accept
|
|
36
|
+
friendly aliases that the package translates to the API's internal names
|
|
37
|
+
(e.g. `"hs4"` → `heading`, `"transport_mode"` → `via`).
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
# Exports to China (160), grouped by HS4 product heading
|
|
41
|
+
soy = comexpy.comex_export(
|
|
42
|
+
start_period="2024-01",
|
|
43
|
+
end_period="2024-12",
|
|
44
|
+
details=["country", "hs4"],
|
|
45
|
+
filters={"country": 160},
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Discover what you can query
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
comexpy.comex_details("general") # available grouping fields
|
|
53
|
+
comexpy.comex_filters("general") # available filters
|
|
54
|
+
comexpy.comex_metrics("general") # available metrics
|
|
55
|
+
|
|
56
|
+
# Look up codes
|
|
57
|
+
comexpy.comex_countries(search="china")
|
|
58
|
+
comexpy.comex_ncm(search="soja", per_page=10)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Messages and rate limiting
|
|
62
|
+
|
|
63
|
+
The ComexStat API rate-limits aggressively. Tune the HTTP behaviour and
|
|
64
|
+
silence progress messages as needed:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
comexpy.set_options(retry_time=30, max_tries=5)
|
|
68
|
+
comexpy.set_verbose(False)
|
|
69
|
+
```
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# User guide
|
|
2
|
+
|
|
3
|
+
## The three datasets
|
|
4
|
+
|
|
5
|
+
| Function | Period | Product detail | Metrics |
|
|
6
|
+
|----------|--------|----------------|---------|
|
|
7
|
+
| `comex_query` / `comex_export` / `comex_import` | 1997–present | NCM, HS2/4/6, CGCE, SITC, ISIC | FOB, KG, statistic, freight, insurance, CIF |
|
|
8
|
+
| `comex_query_city` | 1997–present | HS2/HS4 only | FOB, KG |
|
|
9
|
+
| `comex_historical` | 1989–1996 | NBM | FOB, KG |
|
|
10
|
+
|
|
11
|
+
### General trade
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
comexpy.comex_query(
|
|
15
|
+
flow="export",
|
|
16
|
+
start_period="2023-01",
|
|
17
|
+
end_period="2023-12",
|
|
18
|
+
details=["ncm", "country"],
|
|
19
|
+
filters={"country": [160, 249]},
|
|
20
|
+
month_detail=True,
|
|
21
|
+
metric_cif=True,
|
|
22
|
+
)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### City-level
|
|
26
|
+
|
|
27
|
+
City information is based on the **declarant** of exports/imports, not the
|
|
28
|
+
producer or buyer. Only FOB and KG metrics are available, and product detail
|
|
29
|
+
goes only down to HS4.
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
comexpy.comex_query_city(
|
|
33
|
+
flow="export",
|
|
34
|
+
start_period="2023-01",
|
|
35
|
+
end_period="2023-12",
|
|
36
|
+
details=["country", "state"],
|
|
37
|
+
filters={"state": 26},
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Historical (1989–1996)
|
|
42
|
+
|
|
43
|
+
Uses the NBM nomenclature. Details are limited to `country`, `state` and
|
|
44
|
+
`nbm`.
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
comexpy.comex_historical(
|
|
48
|
+
flow="export",
|
|
49
|
+
start_period="1995-01",
|
|
50
|
+
end_period="1996-12",
|
|
51
|
+
details="country",
|
|
52
|
+
)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Detail and filter aliases
|
|
56
|
+
|
|
57
|
+
The package maps user-friendly names to the API's internal names. You can
|
|
58
|
+
always pass the API name verbatim instead.
|
|
59
|
+
|
|
60
|
+
| Alias | API name |
|
|
61
|
+
|-------|----------|
|
|
62
|
+
| `bloc` / `economic_block` | `economicBlock` |
|
|
63
|
+
| `transport_mode` | `via` |
|
|
64
|
+
| `customs_unit` | `urf` |
|
|
65
|
+
| `hs6` / `sh6` | `subHeading` |
|
|
66
|
+
| `hs4` / `sh4` | `heading` |
|
|
67
|
+
| `hs2` / `sh2` | `chapter` |
|
|
68
|
+
| `cgce_n1/2/3` | `BECLevel1/2/3` |
|
|
69
|
+
| `sitc_section` … | `SITCSection` … |
|
|
70
|
+
| `isic_section` … | `ISICSection` … |
|
|
71
|
+
|
|
72
|
+
The full table is exposed as `comexpy.DETAILS_MAP`.
|
|
73
|
+
|
|
74
|
+
## Auxiliary tables
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
# Geography
|
|
78
|
+
comexpy.comex_countries()
|
|
79
|
+
comexpy.comex_blocs(language="pt")
|
|
80
|
+
comexpy.comex_states()
|
|
81
|
+
comexpy.comex_cities()
|
|
82
|
+
comexpy.comex_transport_modes()
|
|
83
|
+
comexpy.comex_customs_units()
|
|
84
|
+
|
|
85
|
+
# Products
|
|
86
|
+
comexpy.comex_ncm(search="animal")
|
|
87
|
+
comexpy.comex_nbm()
|
|
88
|
+
comexpy.comex_hs(add="ncm")
|
|
89
|
+
|
|
90
|
+
# Classifications
|
|
91
|
+
comexpy.comex_cgce()
|
|
92
|
+
comexpy.comex_sitc(search="carne")
|
|
93
|
+
comexpy.comex_isic("division")
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Every `*_detail` function (e.g. `comex_country_detail(105)`) returns a single
|
|
97
|
+
record as a `dict`.
|
|
98
|
+
|
|
99
|
+
## Configuration
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
comexpy.set_options(
|
|
103
|
+
timeout_get=60, # seconds, GET requests
|
|
104
|
+
timeout_post=120, # seconds, POST requests
|
|
105
|
+
max_tries=3, # attempts per request
|
|
106
|
+
retry_time=10, # seconds between retries (API recommends 10)
|
|
107
|
+
ssl_verify=True, # set False to skip ICP-Brasil verification
|
|
108
|
+
)
|
|
109
|
+
comexpy.set_verbose(False) # silence progress/success messages
|
|
110
|
+
```
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# comexpy
|
|
2
|
+
|
|
3
|
+
**Access the Brazilian Foreign Trade Statistics API (ComexStat) from Python.**
|
|
4
|
+
|
|
5
|
+
`comexpy` is a [pandas](https://pandas.pydata.org/)-friendly interface to the
|
|
6
|
+
[ComexStat API](https://api-comexstat.mdic.gov.br/docs#/) of the Brazilian
|
|
7
|
+
Ministry of Development, Industry, Trade and Services (MDIC). Query detailed
|
|
8
|
+
Brazilian export and import data — every fetch returns a `pandas.DataFrame`.
|
|
9
|
+
|
|
10
|
+
It is the Python port of the R package
|
|
11
|
+
[`comexr`](https://github.com/StrategicProjects/comexr); the public function
|
|
12
|
+
names mirror the R API so knowledge transfers directly between the two.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install comexpy
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## A first query
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import comexpy
|
|
24
|
+
|
|
25
|
+
# Exports by country in January 2024
|
|
26
|
+
df = comexpy.comex_export(
|
|
27
|
+
start_period="2024-01",
|
|
28
|
+
end_period="2024-01",
|
|
29
|
+
details="country",
|
|
30
|
+
)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Continue with the [Getting started](getting-started.md) guide, the
|
|
34
|
+
[User guide](guide.md), or jump to the full [API reference](reference.md).
|
|
35
|
+
|
|
36
|
+
## Highlights
|
|
37
|
+
|
|
38
|
+
- **Tidy output** — every query and table function returns a
|
|
39
|
+
`pandas.DataFrame`.
|
|
40
|
+
- **Three datasets** — general trade (1997–present), city-level, and
|
|
41
|
+
historical (1989–1996).
|
|
42
|
+
- **Friendly aliases** — pass `"hs4"`, `"transport_mode"`, `"cgce_n1"`; the
|
|
43
|
+
package translates them to the API's internal names.
|
|
44
|
+
- **Resilient HTTP** — configurable retry/timeout for the API's aggressive
|
|
45
|
+
rate limiting, plus SSL auto-fallback for ICP-Brasil certificates.
|