get_time_series_data
Retrieve time series data from the Banco Central de Reserva del Perú by specifying a code and date range. Returns formatted data as a list of dates and corresponding values for analysis.
Instructions
Get the data for a specific time series within a date range.
This function retrieves time series data from the BCRP (Banco Central de Reserva del Perú) database for a specific time series code within the specified date range. The data is returned as a list of lists with dates formatted as 'YYYY-MM-DD'.
Args: time_series_code (str): The unique code identifier for the time series. start (str): The start date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. end (str): The end date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data.
Returns: List[List[str]]: A list of lists where each inner list contains: [formatted_date, time_series_value] The date is formatted as 'YYYY-MM-DD' and the value is the corresponding data point for that date.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end | Yes | ||
| start | Yes | ||
| time_series_code | Yes |
Implementation Reference
- main.py:71-118 (handler)The handler function decorated with @mcp.tool(), which registers and defines the implementation for fetching time series data from the BCRP API using the provided code, start, and end dates. Handles JSON parsing and date formatting.@mcp.tool() def get_time_series_data(time_series_code: str, start: str, end: str) -> List[List[str]]: """ Get the data for a specific time series within a date range. This function retrieves time series data from the BCRP (Banco Central de Reserva del Perú) database for a specific time series code within the specified date range. The data is returned as a list of lists with dates formatted as 'YYYY-MM-DD'. Args: time_series_code (str): The unique code identifier for the time series. start (str): The start date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. end (str): The end date for the data retrieval. Format should be '2020-1' for monthly data or '2020-1-1' for daily data. Returns: List[List[str]]: A list of lists where each inner list contains: [formatted_date, time_series_value] The date is formatted as 'YYYY-MM-DD' and the value is the corresponding data point for that date. """ try: url = f"{API_ENDPOINT}/{time_series_code}/json/{start}/{end}/eng" response = requests.get(url) if response.status_code != 200: return [] # Extract JSON from response that may contain HTML errors response_text = response.text json_start = response_text.find('{\n"config":') if json_start == -1: return [] data = json.loads(response_text[json_start:]) result = [] for period in data["periods"]: date_formatted = pd.to_datetime(period["name"]).strftime('%Y-%m-%d') value = period["values"][0] if period["values"] else "n.d." result.append([date_formatted, str(value) if value != "n.d." else "n.d."]) return result except Exception as e: return []