tukan-python 0.2.1__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.
- tukan_python-0.2.1/LICENSE +21 -0
- tukan_python-0.2.1/PKG-INFO +351 -0
- tukan_python-0.2.1/README.md +323 -0
- tukan_python-0.2.1/pyproject.toml +48 -0
- tukan_python-0.2.1/setup.cfg +4 -0
- tukan_python-0.2.1/tests/test_query.py +210 -0
- tukan_python-0.2.1/tukan_python/__init__.py +5 -0
- tukan_python-0.2.1/tukan_python/query.py +209 -0
- tukan_python-0.2.1/tukan_python/tukan.py +218 -0
- tukan_python-0.2.1/tukan_python.egg-info/PKG-INFO +351 -0
- tukan_python-0.2.1/tukan_python.egg-info/SOURCES.txt +12 -0
- tukan_python-0.2.1/tukan_python.egg-info/dependency_links.txt +1 -0
- tukan_python-0.2.1/tukan_python.egg-info/requires.txt +3 -0
- tukan_python-0.2.1/tukan_python.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 TukanMx
|
|
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.
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tukan_python
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: SDK de Python para acceder a datos oficiales de México a través de la API de Tukan.
|
|
5
|
+
Author-email: TukanMx <contacto@tukanmx.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://tukanmx.com
|
|
8
|
+
Project-URL: Repository, https://github.com/TukanMx/tukan_python
|
|
9
|
+
Project-URL: Documentation, https://github.com/TukanMx/tukan_python#readme
|
|
10
|
+
Keywords: tukan,mexico,data,api,inegi,banxico,cnbv,analytics
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: Science/Research
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Operating System :: OS Independent
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Information Analysis
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: pandas>=1.5.0
|
|
25
|
+
Requires-Dist: requests>=2.28.0
|
|
26
|
+
Requires-Dist: python-dotenv>=1.0.0
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# Tukan Python SDK
|
|
30
|
+
|
|
31
|
+
[](https://badge.fury.io/py/tukan_python)
|
|
32
|
+
[](https://www.python.org/downloads/)
|
|
33
|
+
[](https://opensource.org/licenses/MIT)
|
|
34
|
+
|
|
35
|
+
[Tukan](https://tukanmx.com) es la plataforma de datos más completa de México.
|
|
36
|
+
|
|
37
|
+
Si estás en búsqueda de una forma sencilla y eficiente para acceder a todas las estadísticas oficiales de la economía mexicana desde Python, esta es la solución para ti.
|
|
38
|
+
|
|
39
|
+
## Descripción
|
|
40
|
+
|
|
41
|
+
Tukan agrega y estandariza fuentes oficiales de datos públicos como INEGI, Banxico, CNBV, CNSF, CONSAR, entre otras.
|
|
42
|
+
|
|
43
|
+
Este SDK permite consultar y analizar estos datos de forma sencilla desde Python.
|
|
44
|
+
|
|
45
|
+
Para acceder a la gran variedad de datos disponibles es necesario contar con un token y una suscripción activa en [Tukan](https://tukanmx.com). Sin embargo, algunas tablas pueden ser consultadas de forma gratuita.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
## Instalación
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install tukan_python
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Autenticación
|
|
55
|
+
|
|
56
|
+
Para obtener tu token debes primero regidstrarte en [Tukan](https://app.tukanmx.com/user/register).
|
|
57
|
+
|
|
58
|
+
Luego, podrás encontrar tu token en el [panel de usuario](https://app.tukanmx.com/account/).
|
|
59
|
+
|
|
60
|
+

|
|
61
|
+
|
|
62
|
+
Una vez obtenido tu token, puedes configurarlo como una variable de entorno
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
export API_TUKAN="tu_token_aqui"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
o en un archivo `.env`:
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
API_TUKAN=tu_token_aqui
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Otra alternativa es pasar el token directamente al inicializar el cliente:
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from tukan_python import Tukan
|
|
78
|
+
|
|
79
|
+
tukan = Tukan(token="tu_token_aqui")
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Inicio rápido
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from tukan_python import Query
|
|
86
|
+
|
|
87
|
+
# Consultar tipo de cambio FIX (MXN por USD)
|
|
88
|
+
q = Query()
|
|
89
|
+
q.set_table_name("mex_banxico_cf102")
|
|
90
|
+
q.add_date_filter("date", "2025-01-01", "2025-01-15")
|
|
91
|
+
q.add_date_reference_to_group_by("date", level="as_is")
|
|
92
|
+
q.add_aggregate("be08668718242ff", ["identity"]) # Tipo de cambio FIX
|
|
93
|
+
q.set_language("es")
|
|
94
|
+
|
|
95
|
+
resultado = q.execute_query()
|
|
96
|
+
print(resultado["df"])
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Salida:
|
|
100
|
+
```
|
|
101
|
+
date indicator value
|
|
102
|
+
0 2025-01-02 be08668718242ff 20.6917
|
|
103
|
+
1 2025-01-03 be08668718242ff 20.6708
|
|
104
|
+
2 2025-01-06 be08668718242ff 20.3195
|
|
105
|
+
3 2025-01-07 be08668718242ff 20.3440
|
|
106
|
+
4 2025-01-08 be08668718242ff 20.3823
|
|
107
|
+
...
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Ejemplos
|
|
111
|
+
|
|
112
|
+
### 1. Explorar tablas disponibles
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from tukan_python import Tukan
|
|
116
|
+
|
|
117
|
+
tukan = Tukan()
|
|
118
|
+
|
|
119
|
+
# Listar todas las tablas
|
|
120
|
+
tablas = tukan.all_tables()
|
|
121
|
+
print(f"Tablas disponibles: {len(tablas)}")
|
|
122
|
+
|
|
123
|
+
# Filtrar tablas de CNBV y ordenar alfabéticamente
|
|
124
|
+
cnbv = [t for t in tablas if t["id"].startswith("mex_cnbv")]
|
|
125
|
+
cnbv.sort(key=lambda x: x["id"])
|
|
126
|
+
|
|
127
|
+
for t in cnbv[:5]:
|
|
128
|
+
print(f"- {t['id']}: {t['name']}")
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Salida:
|
|
132
|
+
```
|
|
133
|
+
Tablas disponibles: 150+
|
|
134
|
+
- mex_cnbv_cb_balance_sheet_ifrs9: Instituciones de Banca Múltiple - Balance General Detallado (IFRS9)
|
|
135
|
+
- mex_cnbv_cb_capital_ratios: Instituciones de Banca Múltiple - Índice de Capitalización
|
|
136
|
+
- mex_cnbv_cb_ccl: Instituciones de Banca Múltiple - Coeficiente de Cobertura de Liquidez
|
|
137
|
+
- mex_cnbv_cb_claims_by_channel_and_status: Instituciones de Banca Múltiple - Reclamaciones por Estatus y Canal
|
|
138
|
+
- mex_cnbv_cb_clients_per_product: Clientes por Producto Financiero
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 2. Obtener metadata de una tabla
|
|
142
|
+
|
|
143
|
+
Antes de consultar datos, es útil explorar la estructura de la tabla:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
from tukan_python import Tukan
|
|
147
|
+
|
|
148
|
+
tukan = Tukan()
|
|
149
|
+
|
|
150
|
+
# Obtener metadata
|
|
151
|
+
meta = tukan.get_table_metadata("mex_cnbv_cb_orig_by_gender_monthly", language="es")
|
|
152
|
+
|
|
153
|
+
print("Nombre:", meta["data_table"]["name"])
|
|
154
|
+
print("Referencias:", [r["id"] for r in meta["data_table_references"]])
|
|
155
|
+
print("Rango de fechas:", meta["data_table"]["date_ranges"])
|
|
156
|
+
|
|
157
|
+
# Ver indicadores disponibles
|
|
158
|
+
indicadores = tukan.all_indicators_for_table("mex_cnbv_cb_orig_by_gender_monthly")
|
|
159
|
+
for ind in indicadores:
|
|
160
|
+
print(f"- {ind['ref']}: {ind['name']}")
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Salida:
|
|
164
|
+
```
|
|
165
|
+
Nombre: Banca Múltiple - Colocación de Créditos Empresariales y de Vivienda, por Sexo
|
|
166
|
+
Referencias: ['end_date', 'sex', 'start_date', 'institutions', 'geography', ...]
|
|
167
|
+
Rango de fechas: {'end_date': {'max': '2025-11-30', 'min': '2019-12-31'}}
|
|
168
|
+
|
|
169
|
+
- 05451c0b6d5ea78: Monto colocado
|
|
170
|
+
- 78256b18c54451f: Número de créditos
|
|
171
|
+
- b577c6dfc51ebef: Tasa ponderada
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 3. Consultar colocación de crédito bancario por sexo
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
from tukan_python import Query
|
|
178
|
+
|
|
179
|
+
q = Query()
|
|
180
|
+
q.set_table_name("mex_cnbv_cb_orig_by_gender_monthly")
|
|
181
|
+
q.add_date_filter("end_date", "2024-01-01", "2024-06-30")
|
|
182
|
+
q.add_standard_filter("institutions", ["0c959ae6bc0d063"]) # Banca múltiple (agregado)
|
|
183
|
+
q.add_date_reference_to_group_by("end_date", level="monthly")
|
|
184
|
+
q.add_non_date_reference_to_group_by("sex")
|
|
185
|
+
q.add_aggregate("05451c0b6d5ea78", ["sum"]) # Monto colocado
|
|
186
|
+
q.set_language("es")
|
|
187
|
+
|
|
188
|
+
resultado = q.execute_query()
|
|
189
|
+
print(resultado["df"])
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Salida:
|
|
193
|
+
```
|
|
194
|
+
sex__ref end_date indicator value sex
|
|
195
|
+
0 34e63c6a4f88758 2024-01-01 05451c0b6d5ea78__sum 1.468993e+10 Femenino
|
|
196
|
+
1 34e63c6a4f88758 2024-02-01 05451c0b6d5ea78__sum 1.721803e+10 Femenino
|
|
197
|
+
2 653a519004568cb 2024-01-01 05451c0b6d5ea78__sum 3.000290e+10 Masculino
|
|
198
|
+
3 653a519004568cb 2024-02-01 05451c0b6d5ea78__sum 3.302666e+10 Masculino
|
|
199
|
+
...
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### 4. Consultar el INPC (inflación)
|
|
203
|
+
|
|
204
|
+
El INPC requiere filtrar por producto. Primero exploramos el catálogo:
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
from tukan_python import Tukan, Query
|
|
208
|
+
|
|
209
|
+
tukan = Tukan()
|
|
210
|
+
|
|
211
|
+
# Ver productos disponibles
|
|
212
|
+
productos = tukan.get_reference_flat_tree(
|
|
213
|
+
table_name="mex_inegi_inpc_original_product_monthly",
|
|
214
|
+
reference="mex_inegi_cpi_product_structure",
|
|
215
|
+
only_in_table=True
|
|
216
|
+
)
|
|
217
|
+
print(productos[["ref", "name"]].head(10))
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
Salida:
|
|
221
|
+
```
|
|
222
|
+
ref name
|
|
223
|
+
0 9329306b0b5268c Todos los productos
|
|
224
|
+
1 a38da228dc862e7 Alimentos, bebidas y tabaco
|
|
225
|
+
2 714d22fe124b834 Alimentos
|
|
226
|
+
3 1c70d647c151be7 Vivienda
|
|
227
|
+
4 da9ee7065e99719 Transporte
|
|
228
|
+
...
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Ahora consultamos el índice general:
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
q = Query()
|
|
235
|
+
q.set_table_name("mex_inegi_inpc_original_product_monthly")
|
|
236
|
+
q.add_date_filter("date", "2024-01-01", "2024-06-30")
|
|
237
|
+
q.add_standard_filter("mex_inegi_cpi_product_structure", ["9329306b0b5268c"]) # Todos los productos
|
|
238
|
+
q.add_date_reference_to_group_by("date", level="monthly")
|
|
239
|
+
q.add_aggregate("c572db59b8cd109", ["identity"]) # INPC
|
|
240
|
+
q.set_language("es")
|
|
241
|
+
|
|
242
|
+
resultado = q.execute_query()
|
|
243
|
+
print(resultado["df"])
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
Salida:
|
|
247
|
+
```
|
|
248
|
+
date indicator value
|
|
249
|
+
0 2024-01-01 c572db59b8cd109 133.555
|
|
250
|
+
1 2024-02-01 c572db59b8cd109 133.681
|
|
251
|
+
2 2024-03-01 c572db59b8cd109 134.065
|
|
252
|
+
3 2024-04-01 c572db59b8cd109 134.336
|
|
253
|
+
4 2024-05-01 c572db59b8cd109 134.087
|
|
254
|
+
5 2024-06-01 c572db59b8cd109 134.594
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### 5. Explorar catálogos jerárquicos
|
|
258
|
+
|
|
259
|
+
Las referencias estándar tienen estructura jerárquica (país → estado → municipio). El catálogo incluye columnas importantes como `raw` (ID original de la fuente) e `in_table` (si el valor tiene datos en la tabla):
|
|
260
|
+
|
|
261
|
+
```python
|
|
262
|
+
from tukan_python import Tukan
|
|
263
|
+
|
|
264
|
+
tukan = Tukan()
|
|
265
|
+
|
|
266
|
+
# Obtener catálogo de geografía
|
|
267
|
+
df_geo = tukan.get_reference_flat_tree(
|
|
268
|
+
table_name="mex_inegi_census_people_reduced",
|
|
269
|
+
reference="geography"
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
print(df_geo[["raw", "ref", "name", "parent_ref", "in_table"]].head(10))
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Salida:
|
|
276
|
+
```
|
|
277
|
+
raw ref name parent_ref in_table
|
|
278
|
+
0 wd 2064d512d0da97d Mundo FALSE False
|
|
279
|
+
1 na e5fc8e04967fe49 Norteamérica 2064d512d0da97d False
|
|
280
|
+
2 mex b815762a2c6a283 México e5fc8e04967fe49 True
|
|
281
|
+
3 mex_10 db3b32c946ffd13 Durango b815762a2c6a283 True
|
|
282
|
+
4 mex_21 bd8b4a37deee845 Puebla b815762a2c6a283 True
|
|
283
|
+
5 mex_11 a3aa918bd45ac53 Guanajuato b815762a2c6a283 True
|
|
284
|
+
6 mex_24 468bc66c95ecfe6 San Luis Potosí b815762a2c6a283 True
|
|
285
|
+
...
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
La columna `raw` contiene el ID original (ej: `mex_10` para Durango), mientras que `ref` es el ID interno de Tukan. Usa `only_in_table=True` para filtrar solo valores con datos.
|
|
289
|
+
|
|
290
|
+
### 6. Motor Blizzard para consultas pesadas
|
|
291
|
+
|
|
292
|
+
Para consultas con grandes volúmenes de datos, usa el motor Blizzard:
|
|
293
|
+
|
|
294
|
+
```python
|
|
295
|
+
from tukan_python import Query
|
|
296
|
+
|
|
297
|
+
q = Query(engine="blizzard")
|
|
298
|
+
q.set_table_name("mex_cnbv_cb_orig_by_gender_monthly")
|
|
299
|
+
# ... configurar filtros y agregaciones ...
|
|
300
|
+
resultado = q.execute_query()
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Conceptos clave
|
|
304
|
+
|
|
305
|
+
### Tablas
|
|
306
|
+
Cada tabla representa una fuente de datos específica. Los IDs siguen el patrón general:
|
|
307
|
+
`{pais}_{fuente}_{dataset}`
|
|
308
|
+
|
|
309
|
+
Ejemplos:
|
|
310
|
+
- `mex_inegi_inpc_original_product_monthly` - INPC de INEGI
|
|
311
|
+
- `mex_cnbv_cb_orig_by_gender_monthly` - Colocación de créditos de CNBV
|
|
312
|
+
- `mex_inegi_census_people_reduced` - Censo de población de INEGI
|
|
313
|
+
- `mex_shcp_budget_expenditures_by_fc` - Gastos presupuestarios de SHCP
|
|
314
|
+
|
|
315
|
+
### Indicadores
|
|
316
|
+
Son las métricas o valores que se pueden consultar. Cada tabla tiene sus propios indicadores con IDs únicos (ej: `c572db59b8cd109` para el INPC).
|
|
317
|
+
|
|
318
|
+
### Referencias
|
|
319
|
+
Son las dimensiones que contextualizan los datos:
|
|
320
|
+
- **date**: Fechas (pueden tener diferentes nombres como `date`, `end_date`, `start_date`)
|
|
321
|
+
- **standard**: Catálogos jerárquicos como `geography`, `sex`, `economic_activity`
|
|
322
|
+
- **free**: Texto libre
|
|
323
|
+
- **numeric**: Valores numéricos adicionales
|
|
324
|
+
|
|
325
|
+
### Operaciones de agregación
|
|
326
|
+
- `identity`: Valor original sin modificar
|
|
327
|
+
- `sum`: Suma de valores
|
|
328
|
+
- `avg`: Promedio
|
|
329
|
+
|
|
330
|
+
## Fuentes de datos
|
|
331
|
+
|
|
332
|
+
Tukan integra datos de múltiples fuentes oficiales mexicanas:
|
|
333
|
+
|
|
334
|
+
| Fuente | Datos disponibles |
|
|
335
|
+
|--------|-------------------|
|
|
336
|
+
| **INEGI** | Censos, INPC, PIB, encuestas económicas |
|
|
337
|
+
| **Banxico** | Tasas de interés, tipo de cambio, agregados monetarios |
|
|
338
|
+
| **CNBV** | Estados financieros de bancos, SOFOMES, aseguradoras |
|
|
339
|
+
| **SHCP** | Finanzas públicas, presupuesto, deuda |
|
|
340
|
+
| **CONAPO** | Proyecciones de población |
|
|
341
|
+
|
|
342
|
+
## Licencia
|
|
343
|
+
|
|
344
|
+
MIT License - ver [LICENSE](LICENSE) para más detalles.
|
|
345
|
+
|
|
346
|
+
## Links
|
|
347
|
+
|
|
348
|
+
- [Sitio web](https://tukanmx.com)
|
|
349
|
+
- [Documentación](https://github.com/TukanMx/tukan_python#readme)
|
|
350
|
+
- [Reportar issues](https://github.com/TukanMx/tukan_python/issues)
|
|
351
|
+
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
# Tukan Python SDK
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/py/tukan_python)
|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
[Tukan](https://tukanmx.com) es la plataforma de datos más completa de México.
|
|
8
|
+
|
|
9
|
+
Si estás en búsqueda de una forma sencilla y eficiente para acceder a todas las estadísticas oficiales de la economía mexicana desde Python, esta es la solución para ti.
|
|
10
|
+
|
|
11
|
+
## Descripción
|
|
12
|
+
|
|
13
|
+
Tukan agrega y estandariza fuentes oficiales de datos públicos como INEGI, Banxico, CNBV, CNSF, CONSAR, entre otras.
|
|
14
|
+
|
|
15
|
+
Este SDK permite consultar y analizar estos datos de forma sencilla desde Python.
|
|
16
|
+
|
|
17
|
+
Para acceder a la gran variedad de datos disponibles es necesario contar con un token y una suscripción activa en [Tukan](https://tukanmx.com). Sin embargo, algunas tablas pueden ser consultadas de forma gratuita.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## Instalación
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install tukan_python
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Autenticación
|
|
27
|
+
|
|
28
|
+
Para obtener tu token debes primero regidstrarte en [Tukan](https://app.tukanmx.com/user/register).
|
|
29
|
+
|
|
30
|
+
Luego, podrás encontrar tu token en el [panel de usuario](https://app.tukanmx.com/account/).
|
|
31
|
+
|
|
32
|
+

|
|
33
|
+
|
|
34
|
+
Una vez obtenido tu token, puedes configurarlo como una variable de entorno
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
export API_TUKAN="tu_token_aqui"
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
o en un archivo `.env`:
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
API_TUKAN=tu_token_aqui
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Otra alternativa es pasar el token directamente al inicializar el cliente:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from tukan_python import Tukan
|
|
50
|
+
|
|
51
|
+
tukan = Tukan(token="tu_token_aqui")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Inicio rápido
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from tukan_python import Query
|
|
58
|
+
|
|
59
|
+
# Consultar tipo de cambio FIX (MXN por USD)
|
|
60
|
+
q = Query()
|
|
61
|
+
q.set_table_name("mex_banxico_cf102")
|
|
62
|
+
q.add_date_filter("date", "2025-01-01", "2025-01-15")
|
|
63
|
+
q.add_date_reference_to_group_by("date", level="as_is")
|
|
64
|
+
q.add_aggregate("be08668718242ff", ["identity"]) # Tipo de cambio FIX
|
|
65
|
+
q.set_language("es")
|
|
66
|
+
|
|
67
|
+
resultado = q.execute_query()
|
|
68
|
+
print(resultado["df"])
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Salida:
|
|
72
|
+
```
|
|
73
|
+
date indicator value
|
|
74
|
+
0 2025-01-02 be08668718242ff 20.6917
|
|
75
|
+
1 2025-01-03 be08668718242ff 20.6708
|
|
76
|
+
2 2025-01-06 be08668718242ff 20.3195
|
|
77
|
+
3 2025-01-07 be08668718242ff 20.3440
|
|
78
|
+
4 2025-01-08 be08668718242ff 20.3823
|
|
79
|
+
...
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Ejemplos
|
|
83
|
+
|
|
84
|
+
### 1. Explorar tablas disponibles
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from tukan_python import Tukan
|
|
88
|
+
|
|
89
|
+
tukan = Tukan()
|
|
90
|
+
|
|
91
|
+
# Listar todas las tablas
|
|
92
|
+
tablas = tukan.all_tables()
|
|
93
|
+
print(f"Tablas disponibles: {len(tablas)}")
|
|
94
|
+
|
|
95
|
+
# Filtrar tablas de CNBV y ordenar alfabéticamente
|
|
96
|
+
cnbv = [t for t in tablas if t["id"].startswith("mex_cnbv")]
|
|
97
|
+
cnbv.sort(key=lambda x: x["id"])
|
|
98
|
+
|
|
99
|
+
for t in cnbv[:5]:
|
|
100
|
+
print(f"- {t['id']}: {t['name']}")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Salida:
|
|
104
|
+
```
|
|
105
|
+
Tablas disponibles: 150+
|
|
106
|
+
- mex_cnbv_cb_balance_sheet_ifrs9: Instituciones de Banca Múltiple - Balance General Detallado (IFRS9)
|
|
107
|
+
- mex_cnbv_cb_capital_ratios: Instituciones de Banca Múltiple - Índice de Capitalización
|
|
108
|
+
- mex_cnbv_cb_ccl: Instituciones de Banca Múltiple - Coeficiente de Cobertura de Liquidez
|
|
109
|
+
- mex_cnbv_cb_claims_by_channel_and_status: Instituciones de Banca Múltiple - Reclamaciones por Estatus y Canal
|
|
110
|
+
- mex_cnbv_cb_clients_per_product: Clientes por Producto Financiero
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 2. Obtener metadata de una tabla
|
|
114
|
+
|
|
115
|
+
Antes de consultar datos, es útil explorar la estructura de la tabla:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
from tukan_python import Tukan
|
|
119
|
+
|
|
120
|
+
tukan = Tukan()
|
|
121
|
+
|
|
122
|
+
# Obtener metadata
|
|
123
|
+
meta = tukan.get_table_metadata("mex_cnbv_cb_orig_by_gender_monthly", language="es")
|
|
124
|
+
|
|
125
|
+
print("Nombre:", meta["data_table"]["name"])
|
|
126
|
+
print("Referencias:", [r["id"] for r in meta["data_table_references"]])
|
|
127
|
+
print("Rango de fechas:", meta["data_table"]["date_ranges"])
|
|
128
|
+
|
|
129
|
+
# Ver indicadores disponibles
|
|
130
|
+
indicadores = tukan.all_indicators_for_table("mex_cnbv_cb_orig_by_gender_monthly")
|
|
131
|
+
for ind in indicadores:
|
|
132
|
+
print(f"- {ind['ref']}: {ind['name']}")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Salida:
|
|
136
|
+
```
|
|
137
|
+
Nombre: Banca Múltiple - Colocación de Créditos Empresariales y de Vivienda, por Sexo
|
|
138
|
+
Referencias: ['end_date', 'sex', 'start_date', 'institutions', 'geography', ...]
|
|
139
|
+
Rango de fechas: {'end_date': {'max': '2025-11-30', 'min': '2019-12-31'}}
|
|
140
|
+
|
|
141
|
+
- 05451c0b6d5ea78: Monto colocado
|
|
142
|
+
- 78256b18c54451f: Número de créditos
|
|
143
|
+
- b577c6dfc51ebef: Tasa ponderada
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### 3. Consultar colocación de crédito bancario por sexo
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from tukan_python import Query
|
|
150
|
+
|
|
151
|
+
q = Query()
|
|
152
|
+
q.set_table_name("mex_cnbv_cb_orig_by_gender_monthly")
|
|
153
|
+
q.add_date_filter("end_date", "2024-01-01", "2024-06-30")
|
|
154
|
+
q.add_standard_filter("institutions", ["0c959ae6bc0d063"]) # Banca múltiple (agregado)
|
|
155
|
+
q.add_date_reference_to_group_by("end_date", level="monthly")
|
|
156
|
+
q.add_non_date_reference_to_group_by("sex")
|
|
157
|
+
q.add_aggregate("05451c0b6d5ea78", ["sum"]) # Monto colocado
|
|
158
|
+
q.set_language("es")
|
|
159
|
+
|
|
160
|
+
resultado = q.execute_query()
|
|
161
|
+
print(resultado["df"])
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Salida:
|
|
165
|
+
```
|
|
166
|
+
sex__ref end_date indicator value sex
|
|
167
|
+
0 34e63c6a4f88758 2024-01-01 05451c0b6d5ea78__sum 1.468993e+10 Femenino
|
|
168
|
+
1 34e63c6a4f88758 2024-02-01 05451c0b6d5ea78__sum 1.721803e+10 Femenino
|
|
169
|
+
2 653a519004568cb 2024-01-01 05451c0b6d5ea78__sum 3.000290e+10 Masculino
|
|
170
|
+
3 653a519004568cb 2024-02-01 05451c0b6d5ea78__sum 3.302666e+10 Masculino
|
|
171
|
+
...
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### 4. Consultar el INPC (inflación)
|
|
175
|
+
|
|
176
|
+
El INPC requiere filtrar por producto. Primero exploramos el catálogo:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
from tukan_python import Tukan, Query
|
|
180
|
+
|
|
181
|
+
tukan = Tukan()
|
|
182
|
+
|
|
183
|
+
# Ver productos disponibles
|
|
184
|
+
productos = tukan.get_reference_flat_tree(
|
|
185
|
+
table_name="mex_inegi_inpc_original_product_monthly",
|
|
186
|
+
reference="mex_inegi_cpi_product_structure",
|
|
187
|
+
only_in_table=True
|
|
188
|
+
)
|
|
189
|
+
print(productos[["ref", "name"]].head(10))
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Salida:
|
|
193
|
+
```
|
|
194
|
+
ref name
|
|
195
|
+
0 9329306b0b5268c Todos los productos
|
|
196
|
+
1 a38da228dc862e7 Alimentos, bebidas y tabaco
|
|
197
|
+
2 714d22fe124b834 Alimentos
|
|
198
|
+
3 1c70d647c151be7 Vivienda
|
|
199
|
+
4 da9ee7065e99719 Transporte
|
|
200
|
+
...
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
Ahora consultamos el índice general:
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
q = Query()
|
|
207
|
+
q.set_table_name("mex_inegi_inpc_original_product_monthly")
|
|
208
|
+
q.add_date_filter("date", "2024-01-01", "2024-06-30")
|
|
209
|
+
q.add_standard_filter("mex_inegi_cpi_product_structure", ["9329306b0b5268c"]) # Todos los productos
|
|
210
|
+
q.add_date_reference_to_group_by("date", level="monthly")
|
|
211
|
+
q.add_aggregate("c572db59b8cd109", ["identity"]) # INPC
|
|
212
|
+
q.set_language("es")
|
|
213
|
+
|
|
214
|
+
resultado = q.execute_query()
|
|
215
|
+
print(resultado["df"])
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
Salida:
|
|
219
|
+
```
|
|
220
|
+
date indicator value
|
|
221
|
+
0 2024-01-01 c572db59b8cd109 133.555
|
|
222
|
+
1 2024-02-01 c572db59b8cd109 133.681
|
|
223
|
+
2 2024-03-01 c572db59b8cd109 134.065
|
|
224
|
+
3 2024-04-01 c572db59b8cd109 134.336
|
|
225
|
+
4 2024-05-01 c572db59b8cd109 134.087
|
|
226
|
+
5 2024-06-01 c572db59b8cd109 134.594
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### 5. Explorar catálogos jerárquicos
|
|
230
|
+
|
|
231
|
+
Las referencias estándar tienen estructura jerárquica (país → estado → municipio). El catálogo incluye columnas importantes como `raw` (ID original de la fuente) e `in_table` (si el valor tiene datos en la tabla):
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
from tukan_python import Tukan
|
|
235
|
+
|
|
236
|
+
tukan = Tukan()
|
|
237
|
+
|
|
238
|
+
# Obtener catálogo de geografía
|
|
239
|
+
df_geo = tukan.get_reference_flat_tree(
|
|
240
|
+
table_name="mex_inegi_census_people_reduced",
|
|
241
|
+
reference="geography"
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
print(df_geo[["raw", "ref", "name", "parent_ref", "in_table"]].head(10))
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Salida:
|
|
248
|
+
```
|
|
249
|
+
raw ref name parent_ref in_table
|
|
250
|
+
0 wd 2064d512d0da97d Mundo FALSE False
|
|
251
|
+
1 na e5fc8e04967fe49 Norteamérica 2064d512d0da97d False
|
|
252
|
+
2 mex b815762a2c6a283 México e5fc8e04967fe49 True
|
|
253
|
+
3 mex_10 db3b32c946ffd13 Durango b815762a2c6a283 True
|
|
254
|
+
4 mex_21 bd8b4a37deee845 Puebla b815762a2c6a283 True
|
|
255
|
+
5 mex_11 a3aa918bd45ac53 Guanajuato b815762a2c6a283 True
|
|
256
|
+
6 mex_24 468bc66c95ecfe6 San Luis Potosí b815762a2c6a283 True
|
|
257
|
+
...
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
La columna `raw` contiene el ID original (ej: `mex_10` para Durango), mientras que `ref` es el ID interno de Tukan. Usa `only_in_table=True` para filtrar solo valores con datos.
|
|
261
|
+
|
|
262
|
+
### 6. Motor Blizzard para consultas pesadas
|
|
263
|
+
|
|
264
|
+
Para consultas con grandes volúmenes de datos, usa el motor Blizzard:
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
from tukan_python import Query
|
|
268
|
+
|
|
269
|
+
q = Query(engine="blizzard")
|
|
270
|
+
q.set_table_name("mex_cnbv_cb_orig_by_gender_monthly")
|
|
271
|
+
# ... configurar filtros y agregaciones ...
|
|
272
|
+
resultado = q.execute_query()
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## Conceptos clave
|
|
276
|
+
|
|
277
|
+
### Tablas
|
|
278
|
+
Cada tabla representa una fuente de datos específica. Los IDs siguen el patrón general:
|
|
279
|
+
`{pais}_{fuente}_{dataset}`
|
|
280
|
+
|
|
281
|
+
Ejemplos:
|
|
282
|
+
- `mex_inegi_inpc_original_product_monthly` - INPC de INEGI
|
|
283
|
+
- `mex_cnbv_cb_orig_by_gender_monthly` - Colocación de créditos de CNBV
|
|
284
|
+
- `mex_inegi_census_people_reduced` - Censo de población de INEGI
|
|
285
|
+
- `mex_shcp_budget_expenditures_by_fc` - Gastos presupuestarios de SHCP
|
|
286
|
+
|
|
287
|
+
### Indicadores
|
|
288
|
+
Son las métricas o valores que se pueden consultar. Cada tabla tiene sus propios indicadores con IDs únicos (ej: `c572db59b8cd109` para el INPC).
|
|
289
|
+
|
|
290
|
+
### Referencias
|
|
291
|
+
Son las dimensiones que contextualizan los datos:
|
|
292
|
+
- **date**: Fechas (pueden tener diferentes nombres como `date`, `end_date`, `start_date`)
|
|
293
|
+
- **standard**: Catálogos jerárquicos como `geography`, `sex`, `economic_activity`
|
|
294
|
+
- **free**: Texto libre
|
|
295
|
+
- **numeric**: Valores numéricos adicionales
|
|
296
|
+
|
|
297
|
+
### Operaciones de agregación
|
|
298
|
+
- `identity`: Valor original sin modificar
|
|
299
|
+
- `sum`: Suma de valores
|
|
300
|
+
- `avg`: Promedio
|
|
301
|
+
|
|
302
|
+
## Fuentes de datos
|
|
303
|
+
|
|
304
|
+
Tukan integra datos de múltiples fuentes oficiales mexicanas:
|
|
305
|
+
|
|
306
|
+
| Fuente | Datos disponibles |
|
|
307
|
+
|--------|-------------------|
|
|
308
|
+
| **INEGI** | Censos, INPC, PIB, encuestas económicas |
|
|
309
|
+
| **Banxico** | Tasas de interés, tipo de cambio, agregados monetarios |
|
|
310
|
+
| **CNBV** | Estados financieros de bancos, SOFOMES, aseguradoras |
|
|
311
|
+
| **SHCP** | Finanzas públicas, presupuesto, deuda |
|
|
312
|
+
| **CONAPO** | Proyecciones de población |
|
|
313
|
+
|
|
314
|
+
## Licencia
|
|
315
|
+
|
|
316
|
+
MIT License - ver [LICENSE](LICENSE) para más detalles.
|
|
317
|
+
|
|
318
|
+
## Links
|
|
319
|
+
|
|
320
|
+
- [Sitio web](https://tukanmx.com)
|
|
321
|
+
- [Documentación](https://github.com/TukanMx/tukan_python#readme)
|
|
322
|
+
- [Reportar issues](https://github.com/TukanMx/tukan_python/issues)
|
|
323
|
+
|