Referencia API
Para los detalles de cualquier metodo de bcrpy, se puede hacer una busqueda rapida de algun metodo con Ctrl+F y escribiendo el nombre del metodo
(ejemplo: Ctrl+F “plot”).
Metodos globales
- class bcrpy.scan_columns(df: DataFrame, keyword: str, cutoff: float = 0.65)
Perform a fuzzy search on the column names of the given DataFrame and return columns that closely resemble the specified keyword.
Parameters
- dfpandas.DataFrame
The DataFrame whose columns are to be scanned.
- keywordstr
The keyword to search for in the column names.
- cutofffloat, optional
The similarity cutoff for the fuzzy matching (default is 0.65). Columns with a similarity score above this cutoff will be returned.
Returns
- list of str
A list of column names that closely resemble the specified keyword.
- class bcrpy.save_dataframe(df, filename)
Guarda la informacion de datos almacenados y procesados por Python en un archivo
Parametros
- filename: str
Nombre del archivo para guardar. Si el nombre termina con el sufijo “.csv”, se guarda como archivo CSV, si termina con “.md”, se guarda como archivo Markdown. En otro caso, el archivo se guarda en formato de Python [‘pickle’]
- class bcrpy.load_dataframe(filename)
Carga la informacion de datos almacenados en un archivo a Python
Parametros
- filename: str
Nombre del archivo. Si el nombre termina con el sufijo “.csv”, se carga el archivo CSV. En otro caso, el archivo se carga con el modulo de Python [‘pickle’]
- class bcrpy.save_df_as_sql(df, db_name, table_name='time series')
Saves a DataFrame with time series data to an SQLite database.
Parameters:
df: pandas.DataFrame, the DataFrame containing time series data. db_name: str, the name of the SQLite database file (e.g., ‘database.db’). table_name: str, the name of the table in the database to save the data to.
- class bcrpy.load_from_sqlite(db_name, table_name='time_series')
Load data from the SQLite cache and return as a DataFrame.
Metodos locales a la clase bcrpy.Marco
- class bcrpy.Marco
- __init__()
Main framework for storing variables and executing methods to extract, search, and handle data.
Attributes
- metadatapd.DataFrame
DataFrame to store metadata of statistical series from BCRPData.
- datapd.DataFrame
DataFrame to store extracted data from BCRPData.
- codeslist of str
List of series codes of interest.
- startstr
Start date for the selected series in year-month format (default: ‘2010-1’).
- endstr
End date for the selected series in year-month format (default: ‘2016-9’).
- formatstr
Format for extracting/processing data (default: ‘json’).
- langstr
Selected language (default: ‘ing’ for English). Other option is ‘esp’ for Spanish.
- parameters()
Declare the current state of all constructor variables of the Marco class.
- query(codigo='PD39793AM')
Query series code, printed in JSON format.
- query_dict(codigo='PD39793AM')
- wordsearch(keyword='economia', cutoff=0.65, columnas='all')
Perform a fuzzy search for keywords in the BCRPData metadata.
Parameters
- keywordstr
Keyword to search for in the metadata.
- cutofffloat
Similarity cutoff for the fuzzy matching (default is 0.65).
- columnasstr or list of int
Columns to search in. If ‘all’, search in all columns (default is ‘all’).
- order_columns(hacer=True)
Sub-method to reorder columns according to how they were defined in objeto.codes.
Clases parientes a bcrpy.Marco
- class bcrpy.Fetcher
- GET(codes=[], start=None, end=None, forget=False, order=True, datetime=True, check_codes=False, storage='df')
Extracts selected data from BCRPData based on previously declared variables.
Parameters
- codeslist, optional
List of data series codes to extract. If provided, this list replaces the default list in self.codes and is used for the GET request. If None (default), the existing self.codes list will be used.
- startstr, optional
Start date in ‘YYYY-M’ format for the data series. If provided, this date replaces self.start defined in the constructor, only for this request. If not provided, self.start is used.
- endstr, optional
End date in ‘YYYY-M’ format for the data series. If provided, this date replaces self.end defined in the constructor, only for this request. If not provided, self.end is used.
- forgetbool
If True, resets the cache, retrieving data again even if it already exists in the cache.
- orderbool
Maintains column order declared by the user in object.codes with order=True (default). When order=False, columns follow BCRPData’s default order.
- datetimebool
Format of dates in the pandas.DataFrame. Default: True converts dates from BCRPData’s str(MMM.YYYY) format (e.g., Apr.2022) to Timestamp(YYYY-MM-01), allowing for flexible visual graphs and other data manipulations. False keeps the original str(MMM.YYYY) format from BCRPData.
- check_codesbool
If True, series codes are validated against metadata before making the GET request (default: False).
- storagestr, optional
Data storage format: ‘df’ (DataFrame) or ‘sql’ (SQLite). Controls the format in which the information is stored and returned.
- largeGET(codes=[], start=None, end=None, forget=True, chunk_size=100, turbo=True, nucleos=4, check_codes=False, storage='df')
Extracts selected BCRPData series when the quantity exceeds 100 time series.
Parameters
- codeslist, optional
List of time series codes to retrieve, used in turbo mode (parallel computation). Default is an empty list.
- startstr, optional
Start date in ‘YYYY-M’ format for the data series. If provided, this date replaces self.start defined in the constructor, only for this request. If not provided, self.start is used.
- endstr, optional
End date in ‘YYYY-M’ format for the data series. If provided, this date replaces self.end defined in the constructor, only for this request. If not provided, self.end is used.
- chunk_sizeint, optional
Number of time series to retrieve per chunk. Default is 100.
- turbobool, optional
Indicates whether to use “turbo” mode for parallel extraction. Default is True.
- nucleosint, optional
Number of processor cores to use in “turbo” mode. Default is 4.
- check_codesbool, optional
If True, validates the time series codes against metadata before making the request. Default is False.
- storagestr, optional
Data storage format: df (DataFrame) or sql (SQLite). Controls the format in which the data is stored and returned.
- get_data_for_chunk(chunk)
Helper function for largeGET; Get data for a single chunk.
- load_from_cache(df_cache_filename, sql_cache_filename, forget, storage)
Helper method for GET and largeGET. Loads data from cache based on the specified storage format. Checks if the cache file exists and either loads data from it or clears it based on the forget parameter.
- check_metadata_codes()
Check the self.codes list against the first column of the metadata. Notifies the user if codes are not found in metadata or if there are no valid codes.
- save_to_sqlite(data, header, db_name='cache.db')
Save JSON data directly to an SQLite database in a structured format.
- class bcrpy.MetadataHandler
- get_metadata(filename='metadata.csv')
Extract all metadata from BCRPData.
- load_metadata(filename='metadata.csv')
Load the metadata saved as a .csv file into Python.
- save_metadata(filename='metadata_new.csv')
Save the metadata in self.metadata as a .csv file.
- refine_metadata(filename=False)
Reduce metadata to those belonging to the series codes declared in self.codes.