Skip to content

Commit

Permalink
Agrego sección de uso en python.
Browse files Browse the repository at this point in the history
  • Loading branch information
abenassi committed Aug 24, 2018
1 parent 4022bbf commit 719b096
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ doctoc: ## generate table of contents, doctoc command line tool required
bash fix_github_links.sh docs/api_reference.md
doctoc --github --title " " docs/spreadsheet_integration.md
bash fix_github_links.sh docs/spreadsheet_integration.md
doctoc --github --title " " docs/python_usage.md
bash fix_github_links.sh docs/python_usage.md

swaggerdocs:
wget https://github.com/swagger-api/swagger-ui/archive/master.zip -O temp.zip; unzip -jo temp.zip 'swagger-ui-master/dist/*' -d docs/; rm temp.zip
Expand Down
104 changes: 103 additions & 1 deletion docs/python_usage.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,111 @@
# Usar en python

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


- [Con `requests`](#con-requests)
- [Con `pandas`](#con-pandas)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Con `requests`

Armar una función _wrapper_ que facilite construir llamadas a la API.

```python
import requests
import urllib.parse

def get_api_call(ids, **kwargs):
API_BASE_URL = "https://apis.datos.gob.ar/series/api/"
kwargs["ids"] = ",".join(ids)
return "{}{}?{}".format(API_BASE_URL, "series", urllib.parse.urlencode(kwargs))
```

Una llamada válida a la API debe tener por lo menos un id de una serie válida, y luego puede tener parámetros opcionales.

```python
api_call = get_api_call(["168.1_T_CAMBIOR_D_0_0_26"], start_date="2018-08")
print(api_call)

http://apis.datos.gob.ar/series/api/series?start_date=2018-08&ids=168.1_T_CAMBIOR_D_0_0_26
```

Obtener la respuesta en un diccionario.

```python
result = requests.get(api_call).json()
print(result)

{'data': [['2018-08-01', 27.525],
['2018-08-02', 27.45],
['2018-08-03', 27.29],
['2018-08-04', 27.29],
['2018-08-05', 27.29],
['2018-08-06', 27.33],
['2018-08-07', 27.395],
['2018-08-08', 27.65],
['2018-08-09', 28.11],
['2018-08-10', 29.25],
['2018-08-11', 29.25],
['2018-08-12', 29.25],
['2018-08-13', 29.925],
['2018-08-14', 29.61],
['2018-08-15', 30.0],
['2018-08-16', 29.84]],
'meta': [{'end_date': '2018-08-16',
'frequency': 'day',
'start_date': '2018-08-01'},
{'catalog': {'title': 'Datos Programación Macroeconómica'},
'dataset': {'description': 'Datos de tipo de cambio $-USD - futuro dólar . Con respecto al dólar de Estados Unidos. Frecuencia diaria.',
'issued': '2017-09-28',
'source': 'BCRA, MAE, Rofex y Bloomberg',
'title': 'Tipo de Cambio $-USD - Futuro Dólar'},
'distribution': {'downloadURL': 'http://infra.datos.gob.ar/catalog/sspm/dataset/168/distribution/168.1/download/datos-tipo-cambio-usd-futuro-dolar-frecuencia-diaria.csv',
'title': 'Tipo de cambio $-USD - futuro dólar. Valores diarios'},
'field': {'description': 'Tipo de Cambio BNA (Vendedor)',
'id': '168.1_T_CAMBIOR_D_0_0_26',
'units': 'Pesos argentinos por dólar'}}],
'params': {'identifiers': [{'dataset': '168',
'distribution': '168.1',
'id': '168.1_T_CAMBIOR_D_0_0_26'}],
'ids': '168.1_T_CAMBIOR_D_0_0_26',
'start_date': '2018-08'}}
```

## Con `pandas`

## Con `plotly`
Las llamadas a la API en CSV se pueden leer directamente a un `pandas.DataFrame`.

```python
import pandas as pd

df = pd.read_csv(get_api_call(
["168.1_T_CAMBIOR_D_0_0_26", "101.1_I2NG_2016_M_22", "116.3_TCRMA_0_M_36", "143.3_NO_PR_2004_A_21", "11.3_VMATC_2004_M_12"],
format="csv", start_date=2018
))
```

```
indice_tiempo tipo_cambio_bna_vendedor ipc_2016_nivel_general \
2018-01-01 19.023065 127.0147
2018-02-01 19.835179 130.2913
2018-03-01 20.229355 133.5028
2018-04-01 20.251100 136.9380
2018-05-01 23.600452 139.5800
2018-06-01 26.674333 145.0582
2018-07-01 27.607645 149.1178
tipo_cambio_real_multilateral_actual indice_serie_original construccion
96.628715 144.086686 158.920762
96.121512 138.470530 152.630381
93.062453 155.570021 158.931156
90.715862 152.432629 149.860484
104.302984 160.622476 154.011846
114.546258 NaN NaN
107.105698 NaN NaN
```



<!-- ## Con `plotly` -->
2 changes: 1 addition & 1 deletion docs/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Los ids de las series deben pasarse al parámetro `ids`. Se pueden usar parámet

[![](assets/ejemplo_consulta.png)](http://apis.datos.gob.ar/series/api/series?ids=168.1_T_CAMBIOR_D_0_0_26,103.1_I2N_2016_M_15&format=csv)

Ver la [referencia API](api_reference.md) para consultar la documentación completa de todos los parámetros disponibles.
Ver la [referencia API](reference/api_reference.md) para consultar la documentación completa de todos los parámetros disponibles.

### Generador de consultas

Expand Down

0 comments on commit 719b096

Please sign in to comment.