{ "cells": [ { "cell_type": "markdown", "id": "e00355d3", "metadata": {}, "source": [ "# Examples of Using the `bcchapi` Library\n", "\n", "This notebook shows how to use the `buscar` and `cuadro` methods of the `bcchapi` library to interact with the Central Bank of Chile API.\n", "\n", "## Installation\n", "```bash\n", "pip install bcchapi\n", "```\n", "\n", "## Initial setup\n", "You need valid API Key Token to access the Central Bank API." ] }, { "cell_type": "code", "execution_count": null, "id": "7e9cd9ed", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Librería bcchapi importada y configurada\n" ] } ], "source": [ "# Import the library\n", "import bcchapi\n", "import pandas as pd\n", "\n", "# Create an instance with your token\n", "siete = bcchapi.Siete(token=\"your_token\")\n", "\n", "print(\"bcchapi imported and configured successfully.\")" ] }, { "cell_type": "markdown", "id": "59f8220f", "metadata": {}, "source": [ "---\n", "## Example 1: Search and Query the Observed Dollar\n", "\n", "In this example:\n", "1. We search for series related to \"dólar observado\"\n", "2. We query the data using the specific code\n", "3. We compute basic statistics from the extracted data\n", "4. Extra: We show different options of the `cuadro` method" ] }, { "cell_type": "markdown", "id": "c0b69668", "metadata": {}, "source": [ "### Step 1: Search for the Observed Dollar series" ] }, { "cell_type": "code", "execution_count": 3, "id": "fe270cca", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found 3 series related to 'dólar observado'\n", "\n", "First 5 matches:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
seriesIdfrequencyCodespanishTitle
0F073.TCO.PRE.Z.DDAILYTipo de cambio nominal (dólar observado $CLP/U...
1F073.TCO.PRE.Z.AANNUALTipo de cambio nominal (dólar observado $CLP/U...
2F073.TCO.PRE.HIST.MMONTHLYTipo de cambio del dólar observado diario, ser...
\n", "
" ], "text/plain": [ " seriesId frequencyCode \\\n", "0 F073.TCO.PRE.Z.D DAILY \n", "1 F073.TCO.PRE.Z.A ANNUAL \n", "2 F073.TCO.PRE.HIST.M MONTHLY \n", "\n", " spanishTitle \n", "0 Tipo de cambio nominal (dólar observado $CLP/U... \n", "1 Tipo de cambio nominal (dólar observado $CLP/U... \n", "2 Tipo de cambio del dólar observado diario, ser... " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Search \"dólar observado\" in the available series\n", "search_result = siete.buscar(\"dólar observado\")\n", "print(f\"Found {len(search_result)} series related to 'dólar observado'\")\n", "print(\"\\nFirst 5 matches:\")\n", "search_result[['seriesId', 'frequencyCode', 'spanishTitle']].head()" ] }, { "cell_type": "markdown", "id": "21aaa4cc", "metadata": {}, "source": [ "### Step 2: Query the data for the specific series\n", "\n", "We will use the code: `F073.TCO.PRE.Z.D` (Nominal exchange rate peso/dollar - daily)" ] }, { "cell_type": "code", "execution_count": 4, "id": "817fd7ea", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data retrieved: 394 observations\n", "Period: 2024-09-02 00:00:00 to 2025-09-30 00:00:00\n", "\n", "Last 10 observations:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
F073.TCO.PRE.Z.D
2025-09-21NaN
2025-09-22951.03
2025-09-23954.72
2025-09-24952.87
2025-09-25953.24
2025-09-26956.42
2025-09-27NaN
2025-09-28NaN
2025-09-29958.90
2025-09-30961.24
\n", "
" ], "text/plain": [ " F073.TCO.PRE.Z.D\n", "2025-09-21 NaN\n", "2025-09-22 951.03\n", "2025-09-23 954.72\n", "2025-09-24 952.87\n", "2025-09-25 953.24\n", "2025-09-26 956.42\n", "2025-09-27 NaN\n", "2025-09-28 NaN\n", "2025-09-29 958.90\n", "2025-09-30 961.24" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Query data from the last year\n", "dollar_data = siete.cuadro(\n", " series=[\"F073.TCO.PRE.Z.D\"],\n", " desde=\"2024-09-01\",\n", " hasta=\"2025-09-30\"\n", ")\n", "\n", "print(f\"Data retrieved: {len(dollar_data)} observations\")\n", "print(f\"Period: {dollar_data.index.min()} to {dollar_data.index.max()}\")\n", "print(\"\\nLast 10 observations:\")\n", "dollar_data.tail(10)" ] }, { "cell_type": "markdown", "id": "2f88a356", "metadata": {}, "source": [ "### Step 3: Get basic statistics for the queried series" ] }, { "cell_type": "code", "execution_count": 5, "id": "e3d74d7b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observed Dollar Statistics:\n", "Minimum value: $896.25\n", "Maximum value: $1012.76\n", "Average value: $955.93\n", "Last observation: $961.24\n" ] } ], "source": [ "# Basic statistics\n", "print(\"Observed Dollar Statistics:\")\n", "print(f\"Minimum value: ${dollar_data.min().iloc[0]:.2f}\")\n", "print(f\"Maximum value: ${dollar_data.max().iloc[0]:.2f}\")\n", "print(f\"Average value: ${dollar_data.mean().iloc[0]:.2f}\")\n", "print(f\"Last observation: ${dollar_data.iloc[-1, 0]:.2f}\")" ] }, { "cell_type": "markdown", "id": "0a1c64da", "metadata": {}, "source": [ "### Extra: Using different parameters of the `cuadro` method" ] }, { "cell_type": "code", "execution_count": 6, "id": "e1a0d477", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Observed Dollar - Monthly Average 2024-2025:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Observed Dollar
2024-09-30926.214444
2024-10-31933.812273
2024-11-30971.600000
2024-12-31982.296000
2025-01-311000.763636
2025-02-28956.620000
2025-03-31932.551905
2025-04-30961.957143
2025-05-31941.012500
2025-06-30938.037000
2025-07-31951.550000
2025-08-31966.303500
2025-09-30960.367500
\n", "
" ], "text/plain": [ " Observed Dollar\n", "2024-09-30 926.214444\n", "2024-10-31 933.812273\n", "2024-11-30 971.600000\n", "2024-12-31 982.296000\n", "2025-01-31 1000.763636\n", "2025-02-28 956.620000\n", "2025-03-31 932.551905\n", "2025-04-30 961.957143\n", "2025-05-31 941.012500\n", "2025-06-30 938.037000\n", "2025-07-31 951.550000\n", "2025-08-31 966.303500\n", "2025-09-30 960.367500" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example with custom name and monthly frequency\n", "monthly_dollar_data = siete.cuadro(\n", " series=[\"F073.TCO.PRE.Z.D\"],\n", " desde=\"2024-09-01\",\n", " hasta=\"2025-09-30\",\n", " nombres=[\"Observed Dollar\"],\n", " frecuencia=\"M\", # Monthly\n", " observado=\"mean\" # Monthly average\n", ")\n", "\n", "print(\"Observed Dollar - Monthly Average 2024-2025:\")\n", "monthly_dollar_data" ] }, { "cell_type": "markdown", "id": "7e355323", "metadata": {}, "source": [ "---\n", "## Example 2: GDP and IMACEC with different frequencies\n", "\n", "This example shows the complete handling of series with different frequencies:\n", "- **IMACEC**: Monthly (`F032.IMC.IND.Z.Z.EP18.Z.Z.0.M`)\n", "- **GDP**: Quarterly (`F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T`)\n", "\n", "We will follow these steps:\n", "1. Search for the series\n", "2. Query each series separately\n", "3. Calculate year-on-year changes for each series\n", "4. Query both series" ] }, { "cell_type": "markdown", "id": "d62f96fb", "metadata": {}, "source": [ "### Step 1: Search for the series" ] }, { "cell_type": "code", "execution_count": 7, "id": "002a7497", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Series found for IMACEC: 4\n", "\n", "First matches:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
seriesIdfrequencyCodespanishTitle
0F032.IMC.IND.Z.Z.EP13.Z.Z.0.MMONTHLYImacec empalmado, serie original (índice 2013=...
1F032.IMC.IND.Z.Z.EP13.Z.Z.1.MMONTHLYImacec empalmado, desestacionalizado (índice 2...
2F032.IMC.IND.Z.Z.EP18.Z.Z.0.MMONTHLYImacec empalmado, serie original (índice 2018=...
3F032.IMC.IND.Z.Z.EP18.Z.Z.1.MMONTHLYImacec empalmado, desestacionalizado (índice 2...
\n", "
" ], "text/plain": [ " seriesId frequencyCode \\\n", "0 F032.IMC.IND.Z.Z.EP13.Z.Z.0.M MONTHLY \n", "1 F032.IMC.IND.Z.Z.EP13.Z.Z.1.M MONTHLY \n", "2 F032.IMC.IND.Z.Z.EP18.Z.Z.0.M MONTHLY \n", "3 F032.IMC.IND.Z.Z.EP18.Z.Z.1.M MONTHLY \n", "\n", " spanishTitle \n", "0 Imacec empalmado, serie original (índice 2013=... \n", "1 Imacec empalmado, desestacionalizado (índice 2... \n", "2 Imacec empalmado, serie original (índice 2018=... \n", "3 Imacec empalmado, desestacionalizado (índice 2... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Search IMACEC\n", "result_imacec = siete.buscar(\"Imacec empalmado\")\n", "print(f\"Series found for IMACEC: {len(result_imacec)}\")\n", "print(\"\\nFirst matches:\")\n", "result_imacec[['seriesId', 'frequencyCode', 'spanishTitle']].head(5)" ] }, { "cell_type": "code", "execution_count": 8, "id": "6958b485", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Series found for GDP: 68\n", "\n", "Last matches:\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
seriesIdfrequencyCodespanishTitle
58F032.PIB.FLU.R.CLP.EP08.Z.Z.0.TQUARTERLYPIB, volumen a precios del año anterior encade...
59F032.PIB.FLU.R.CLP.EP13.Z.Z.0.TQUARTERLYPIB, volumen a precios del año anterior encade...
60F032.PIB.FLU.R.CLP.EP18.Z.Z.0.TQUARTERLYPIB, volumen a precios del año anterior encade...
61F032.PIB.FLU.R.CLP.HIST.Z.Z.0.TQUARTERLYPIB, volumen a precios del año anterior encade...
62F032.PIB.FLU.R.CLP.HIST.Z.Z.3.TQUARTERLYPIB, volumen a precios del año anterior encade...
63F032.PIB.FLU.R.CLP.HIST13.Z.Z.0.TQUARTERLYPIB, volumen a precios del año anterior encade...
64F032.PIB.FLU.R.CLP.HIST13.Z.Z.3.TQUARTERLYPIB, volumen a precios del año anterior encade...
65F032.PIB.FLU.R.CLP.HIST.Z.Z.0.AANNUALPIB, volumen a precios del año anterior encade...
66F032.PIB.FLU.R.CLP.HIST13.Z.Z.0.AANNUALPIB, volumen a precios del año anterior encade...
67F032.PIB.FLU.R.CLP.HIST18.Z.Z.0.AANNUALPIB, volumen a precios del año anterior encade...
\n", "
" ], "text/plain": [ " seriesId frequencyCode \\\n", "58 F032.PIB.FLU.R.CLP.EP08.Z.Z.0.T QUARTERLY \n", "59 F032.PIB.FLU.R.CLP.EP13.Z.Z.0.T QUARTERLY \n", "60 F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T QUARTERLY \n", "61 F032.PIB.FLU.R.CLP.HIST.Z.Z.0.T QUARTERLY \n", "62 F032.PIB.FLU.R.CLP.HIST.Z.Z.3.T QUARTERLY \n", "63 F032.PIB.FLU.R.CLP.HIST13.Z.Z.0.T QUARTERLY \n", "64 F032.PIB.FLU.R.CLP.HIST13.Z.Z.3.T QUARTERLY \n", "65 F032.PIB.FLU.R.CLP.HIST.Z.Z.0.A ANNUAL \n", "66 F032.PIB.FLU.R.CLP.HIST13.Z.Z.0.A ANNUAL \n", "67 F032.PIB.FLU.R.CLP.HIST18.Z.Z.0.A ANNUAL \n", "\n", " spanishTitle \n", "58 PIB, volumen a precios del año anterior encade... \n", "59 PIB, volumen a precios del año anterior encade... \n", "60 PIB, volumen a precios del año anterior encade... \n", "61 PIB, volumen a precios del año anterior encade... \n", "62 PIB, volumen a precios del año anterior encade... \n", "63 PIB, volumen a precios del año anterior encade... \n", "64 PIB, volumen a precios del año anterior encade... \n", "65 PIB, volumen a precios del año anterior encade... \n", "66 PIB, volumen a precios del año anterior encade... \n", "67 PIB, volumen a precios del año anterior encade... " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Search GDP\n", "result_gdp = siete.buscar(\"PIB, volumen a precios del año anterior encadenado\")\n", "print(f\"Series found for GDP: {len(result_gdp)}\")\n", "print(\"\\nLast matches:\")\n", "result_gdp[['seriesId', 'frequencyCode', 'spanishTitle']].tail(10)" ] }, { "cell_type": "markdown", "id": "7a23d9d3", "metadata": {}, "source": [ "### Step 2: Query each series separately" ] }, { "cell_type": "code", "execution_count": 9, "id": "459a7373", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IMACEC: 21 monthly observations\n", "Period: 2024-01 to 2025-09\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
IMACEC
2025-05-01112.950836
2025-06-01108.388247
2025-07-01109.039654
2025-08-01110.446073
2025-09-01109.152886
\n", "
" ], "text/plain": [ " IMACEC\n", "2025-05-01 112.950836\n", "2025-06-01 108.388247\n", "2025-07-01 109.039654\n", "2025-08-01 110.446073\n", "2025-09-01 109.152886" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Query IMACEC (monthly)\n", "imacec = siete.cuadro(\n", " series=[\"F032.IMC.IND.Z.Z.EP18.Z.Z.0.M\"],\n", " desde=\"2024-01-01\",\n", " hasta=\"2025-09-30\",\n", " nombres=[\"IMACEC\"]\n", ")\n", "\n", "print(f\"IMACEC: {len(imacec)} monthly observations\")\n", "print(f\"Period: {imacec.index.min().strftime('%Y-%m')} to {imacec.index.max().strftime('%Y-%m')}\")\n", "imacec.tail()" ] }, { "cell_type": "code", "execution_count": null, "id": "68e48e7a", "metadata": {}, "outputs": [], "source": [ "# Query GDP (quarterly)\n", "gdp = siete.cuadro(\n", " series=[\"F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T\"],\n", " desde=\"2024-01-01\",\n", " hasta=\"2025-09-30\",\n", " nombres=[\"GDP\"]\n", ")\n", "\n", "print(f\"GDP: {len(gdp)} quarterly observations\")\n", "print(f\"Period: {gdp.index.min().strftime('%Y-%m')} to {gdp.index.max().strftime('%Y-%m')}\")\n", "gdp" ] }, { "cell_type": "markdown", "id": "739e7ab2", "metadata": {}, "source": [ "### Step 3: Calculate year-on-year changes for each series" ] }, { "cell_type": "code", "execution_count": null, "id": "680138b1", "metadata": {}, "outputs": [], "source": [ "# IMACEC year-on-year change\n", "imacec_var = siete.cuadro(\n", " series=[\"F032.IMC.IND.Z.Z.EP18.Z.Z.0.M\"],\n", " desde=\"2024-01-01\",\n", " hasta=\"2025-09-30\",\n", " nombres=[\"IMACEC_var\"],\n", " variacion=12 # 12 months back\n", ")\n", "\n", "print(\"IMACEC - Year-on-year change (%):\")\n", "(imacec_var * 100).round(2).tail()" ] }, { "cell_type": "code", "execution_count": null, "id": "0919e079", "metadata": {}, "outputs": [], "source": [ "# GDP year-on-year change\n", "gdp_var = siete.cuadro(\n", " series=[\"F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T\"],\n", " desde=\"2024-01-01\",\n", " hasta=\"2025-09-30\",\n", " nombres=[\"GDP_var\"],\n", " variacion=12 # 12 months back\n", ")\n", "\n", "print(\"GDP - Year-on-year change (%):\")\n", "(gdp_var * 100).round(2).tail()" ] }, { "cell_type": "markdown", "id": "2713bda0", "metadata": {}, "source": [ "### Step 4: Query both series with original frequency" ] }, { "cell_type": "code", "execution_count": null, "id": "e81400fb", "metadata": {}, "outputs": [], "source": [ "# Query both series using their original frequency\n", "combined_data = siete.cuadro(\n", " series=[\n", " \"F032.IMC.IND.Z.Z.EP18.Z.Z.0.M\", # IMACEC (monthly)\n", " \"F032.PIB.FLU.R.CLP.EP18.Z.Z.0.T\" # GDP (quarterly)\n", " ],\n", " desde=\"2024-01-01\",\n", " hasta=\"2025-09-30\",\n", " nombres=[\"IMACEC\", \"GDP\"]\n", ")\n", "\n", "print(\"Combined data (original frequency):\")\n", "combined_data.head(8)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }