Sie_banxico - A python class for the Economic Information System (SIE) API of Banco de México

Overview

sie_banxico

PyPi Version

A python class for the Economic Information System (SIE) API of Banco de México.

Args: token (str): A query token from Banco de México id_series (list): A list with the economic series id or with the series id range to query. ** A list must be given even though only one serie is consulted. language (str): Language of the obtained information. 'en' (default) for english or 'es' for spanish

Notes: (1) In order to retrive information from the SIE API, a query token is required. The token can be requested here (2) Each economic serie is related to an unique ID. The full series catalogue can be consulted here

Pypi Installation

pip install sie_banxico

SIEBanxico Class Instance

Querying Monetary Aggregates M1 (SF311408) and M2 (SF311418) Data

 >>> from api_banxico import SIEBanxico
 >>> api = SIEBanxico(token = token, id_series = ['SF311408' ,'SF311418'], language = 'en')

Class documentation and attributes

>>> api.__doc__
'Returns the full class documentation'
>>> api.token
'1b7da065cf574289a2cb511faeXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' # This is an example token
>>> api.series
'SF311408,SF311418'

Methods for modify the arguments of the object

set_token: Change the current query token

>>> api.set_token(token = new_token)

set_id_series: Allows to change the series to query

>>> api.append_id_series(id_series = ['SF311412'])
>>> api.series
'SF311408,SF311418,SF311412'

append_id_series: Allows to update the series to query

>>> api.set_id_series(id_series='SF311408-SF311418')
>>> api.series
'SF311408-SF311418'

GET Request Methods

>>> api = SIEBanxico(token = token, id_series = ['SF311408' ,'SF311418']

get_metadata: Allows to consult metadata of the series

    Allows to consult metadata of the series.
    Returns:
        dict: json response format
>>> api.get_metadata()
{'bmx': {'series': [{'idSerie': 'SF311418', 'titulo': 'Monetary Aggregates M2 = M1 + monetary instruments held by residents', 'fechaInicio': '12/01/2000', 'fechaFin': '11/01/2021', 'periodicidad': 'Monthly', 'cifra': 'Stocks', 'unidad': 'Thousands of Pesos', 'versionada': False}, {'idSerie': 'SF311408', 'titulo': 'Monetary Aggregates M1', 'fechaInicio': '12/01/2000', 'fechaFin': '11/01/2021', 'periodicidad': 'Monthly', 'cifra': 'Stocks', 'unidad': 'Thousands of Pesos', 'versionada': False}]}}

get_lastdata: Returns the most recent published data

Returns the most recent published data for the requested series. Args: pct_change (str, optional): None (default) for levels, "PorcObsAnt" for change rate compared to the previous observation, "PorcAnual" for anual change rate, "PorcAcumAnual" for annual acummulated change rate. Returns: dict: json response format

>>> api.get_lastdata()
{'bmx': {'series': [{'idSerie': 'SF311418', 'titulo': 'Monetary Aggregates M2 = M1 + monetary instruments held by residents', 'datos': [{'fecha': '01/11/2021', 'dato': '11,150,071,721.09'}]}, {'idSerie': 'SF311408', 'titulo': 'Monetary Aggregates M1', 'datos': [{'fecha': '01/11/2021', 'dato': '6,105,266,291.65'}]}]}}

get_timeseries: Allows to consult time series data

    Allows to consult the whole time series data, corresponding to the period defined between the initial date and the final date in the metadata.
    Args:
        pct_change (str, optional): None (default) for levels, "PorcObsAnt" for change rate compared to the previous observation, "PorcAnual" for anual change rate, "PorcAcumAnual" for annual acummulated change rate.
    Returns:
        dict: json response format
>>> api.get_timeseries(pct_change='PorcAnual')
{'bmx': {'series': [{'idSerie': 'SF311418',
    'titulo': 'Monetary Aggregates M2 = M1 + monetary instruments held by residents',
    'datos': [{'fecha': '01/12/2001', 'dato': '12.89'},
     {'fecha': '01/01/2002', 'dato': '13.99'},
     ...
     {'fecha': '01/11/2021', 'dato': '13.38'}],
     'incrementos': 'PorcAnual'}]}}

get_timeseries_range: Returns the data for the period defined

    Returns the data of the requested series, for the defined period.
    Args:
        init_date (str): The date on which the period of obtained data starts. The date must be sent in the format yyyy-mm-dd. If the given date is out of the metadata time range, the oldest value is returned.
        end_date (str): The date on which the period of obtained data concludes. The date must be sent in the format yyyy-mm-dd. If the given date is out of the metadata time range, the most recent value is returned.
        pct_change (str, optional): None (default) for levels, "PorcObsAnt" for change rate compared to the previous observation, "PorcAnual" for anual change rate, "PorcAcumAnual" for annual acummulated change rate.     
    Returns:
        dict: json response format
>>> api.get_timeseries_range(init_date='2000-12-31', end_date='2004-04-01')
{'bmx': {'series': [{'idSerie': 'SF311408',
    'titulo': 'Monetary Aggregates M1',
    'datos': [{'fecha': '01/01/2001', 'dato': '524,836,129.99'},
     {'fecha': '01/02/2001', 'dato': '517,186,605.97'},
     ...
     {'fecha': '01/04/2004', 'dato': '2,306,755,672.89'}]}]}}

Pandas integration for data manipulation (and further analysis)

All the request methods returns a response in json format that can be used with other Python libraries.

The response for the api.get_timeseries_range(init_date='2000-12-31', end_date='2004-04-01') is a nested dictionary, so we need to follow a path to extract the specific values for the series and then transform the data into a pandas object; like a Serie or a DataFrame. For example:

data = api.get_timeseries_range(init_date='2000-12-31', end_date='2004-04-01')

# Extract the Monetary Aggregate M1 data
data['bmx']['series'][0]['datos']
[{'fecha': '01/01/2001', 'dato': '524,836,129.99'},
 ...
 {'fecha': '01/04/2004', 'dato': '799,774,807.43'}]

# Transform the data into a pandas DataDrame
import pandas as pd
df = pd.DataFrame(timeseries_range['bmx']['series'][0]['datos'])
df.head()
        fecha            dato
0  01/01/2001  524,836,129.99
1  01/02/2001  517,186,605.97
2  01/03/2001  509,701,873.04
3  01/04/2001  511,952,430.01
4  01/05/2001  514,845,459.96

Another useful pandas function to transform json formats into a dataframe is 'json_normalize':

df = pd.json_normalize(timeseries_range['bmx']['series'], record_path = 'datos', meta = ['idSerie', 'titulo'])
df['titulo'] = df['titulo'].apply(lambda x: x.replace('Monetary Aggregates M2 = M1 + monetary instruments held by residents', 'Monetary Aggregates M2'))
df.head()
        fecha            dato   idSerie                  titulo
0  01/01/2001  524,836,129.99  SF311408  Monetary Aggregates M1
1  01/02/2001  517,186,605.97  SF311408  Monetary Aggregates M1
2  01/03/2001  509,701,873.04  SF311408  Monetary Aggregates M1
3  01/04/2001  511,952,430.01  SF311408  Monetary Aggregates M1
4  01/05/2001  514,845,459.96  SF311408  Monetary Aggregates M1
df.tail()
         fecha              dato   idSerie                  titulo
75  01/12/2003  2,331,594,974.69  SF311418  Monetary Aggregates M2
76  01/01/2004  2,339,289,328.74  SF311418  Monetary Aggregates M2
77  01/02/2004  2,285,732,239.36  SF311418  Monetary Aggregates M2
78  01/03/2004  2,312,217,167.10  SF311418  Monetary Aggregates M2
79  01/04/2004  2,306,755,672.89  SF311418  Monetary Aggregates M2

Licence

The MIT License (MIT)

By

Dillan Aguirre Sedeño ([email protected])

Owner
Dillan
Dillan
A Discord bot to allow people to create lists of random characters, with limit reroll options.

Mugen Bot A small bot I made to practice python and allow people to publically select random characters on a discord server. Uses py-cord, as that is

Haley 2 Feb 06, 2022
telegram bot that calculates file hash / Dosya toplamı hesaplayan telegram botu

Telegram File Hash Bot FileHashBot: 🇬🇧 Bot that calculates file hashes. 🇹🇷 Dosya toplamları hesaplayan bot. Demo in Telegram: @FileHashBot 🇬🇧 Se

Hüzünlü Artemis [HuzunluArtemis] 5 Jun 29, 2022
A Simple, LightWeight, Statically-Typed Python3 API wrapper for GogoAnime.

AniKimi API A Simple, LightWeight, Statically-Typed Python3 API wrapper for GogoAnime The v2 of gogoanimeapi (depreciated) Made with JavaScript and Py

17 Dec 09, 2022
A full-fledged discord bot with moderation and a lot more.

HOT-BOT-POL-POT ⭐ Star me on GitHub m'lady.... hot-bot-pol-pot is a moderation discord bot written using enhanced-dpy library with many functionalitie

Pure Cheekbones 4 Oct 08, 2022
A python package that fetches tweets and user information in a very pythonic manner.

Tweetsy Tweetsy uses Twitter's underlying API to fetch user information and tweets and present it in a human-friendly way. What makes Tweetsy special

Sakirul Alam 5 Nov 12, 2022
Tools to help record data from Qiskit jobs

archiver4qiskit Tools to help record data from Qiskit jobs. Install with pip install git+https://github.com/NCCR-SPIN/archiver4qiskit.git Import the

0 Dec 10, 2021
A tool to build scripts to toggle between minimal & default services in Windows based on user defined lists.

A tool to build scripts to toggle between minimal & default services in Windows based on user defined lists.

AMIT 29 Jan 01, 2023
An API-driven solution for Makerspaces, Tinkerers, and Hackers.

Mventory is an API-driven inventory solution for Makers, Makerspaces, Hackspaces, and just about anyone else who needs to keep track of "stuff".

Matthew Macdonald-Wallace 107 Dec 21, 2022
CLI tool that checks who does and who does not follow you back on Instagram

CLI tool that checks who does and who does not follow you back on Instagram. It also checks who you don't follow back on Instagram.

Ayushman Roy 3 Dec 02, 2022
Create light scenes , voice control, ifttt, fuzzywuzzy speech correction and much more with Tuya light bulbs.

LightBox Features: Auto discover tuya lights Set and create moods (aka: light profiles) Change moods via IFTTT List moods via IFTTT FuzzyWuzzy, speech

Robert Nagtegaal 1 Dec 20, 2021
A modular telegram Python bot running on python3 with an sqlalchemy database.

Saber A modular telegram Python bot running on python3 with an sqlalchemy database. Originally a marie fork - Saber has evolved further and was built

ZERO • アクバル . 4 Nov 09, 2021
Sakura: an powerfull Autofilter bot that can be used in your groups

Sakura AutoFilter This Bot May Look Like Mwk_AutofilterBot And Its Because I Like Its UI, That's All Sakura is an powerfull Autofilter bot that can be

PaulWalker 12 Oct 21, 2022
An implementation of webhook used to notify GitHub repository events to DingTalk.

GitHub to DingTask An implementation of webhook used to notify GitHub repository events to DingTalk.

Prodesire 5 Oct 02, 2022
Hassium Server Manager For Python

Hassium Server Manager This is meant to be a tool for mostly internal use. I decided that I would make it open souce in case anyone wanted to use it.

0 Nov 24, 2022
Nyon-stream - A python script that uses webtorrent to stream nyaa videos directly to mpv

nyon-stream A rather shitty script that uses webtorrent to stream nyaa videos di

18 Feb 08, 2022
A course on getting started with the Twitter API v2 for academic research

Getting started with the Twitter API v2 for academic research Welcome to this '101 course' on getting started with academic research using the Twitter

@TwitterDev 426 Jan 04, 2023
A simpler way to make forms, surveys, and reaction input using discord.py.

discord-ext-forms An easier way to make forms and surveys in discord.py. This module is a very simple way to ask questions and create complete forms i

thrizzle 16 Nov 06, 2022
A simple python script to send files into your telegram Bot form your PC, Server etc.

telegramSend A simple python script to send files into your telegram Bot form your PC, Server etc. How to Use Install requirements.txt pip3 install -r

Ajay Kumar Tekam 1 Jul 19, 2022
PyHoroscope - Observational Indian lunisolar calendar, horoscope and matching using the Swiss ephemeris

PyHoroscope Observational Indian lunisolar calendar, horoscope and matching usin

4 Jun 05, 2022
Charged's cogs for Red!

Light-Cogs Charged's cogs for Red! Read below for more information. Features and Cogs TechInfo Lots of commands helping you and your devices! Extended

Charged 2 Jan 07, 2022