Pular para conteúdo

IBGE Data

ipca_indexes(start, end)

Retrieves the IPCA index values for a specified date range.

Makes an API call to the IBGE's data portal using the format: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/YYYYMM-YYYYMM/variaveis/2266?localidades=N1[all]

Example: For the date range "01-01-2024" to "31-03-2024", the API URL will be: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/202401-202403/variaveis/2266?localidades=N1[all]

Parameters:

Name Type Description Default
start DateScalar

The start date of the date range

required
end DateScalar

The end date of the date range

required

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame with columns 'Period' and 'Value'

Examples:

>>> from pyield import ibge
>>> # Get the IPCA indexes for the first quarter of 2024
>>> df = ibge.ipca_indexes("01-01-2024", "31-03-2024")
Source code in pyield/ibge/ipca.py
def ipca_indexes(start: DateScalar, end: DateScalar) -> pd.DataFrame:
    """
    Retrieves the IPCA index values for a specified date range.

    Makes an API call to the IBGE's data portal using the format:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/YYYYMM-YYYYMM/variaveis/2266?localidades=N1[all]

    Example: For the date range "01-01-2024" to "31-03-2024", the API URL will be:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/202401-202403/variaveis/2266?localidades=N1[all]

    Args:
        start (DateScalar): The start date of the date range
        end (DateScalar): The end date of the date range

    Returns:
        pd.DataFrame: DataFrame with columns 'Period' and 'Value'

    Examples:
        >>> from pyield import ibge
        >>> # Get the IPCA indexes for the first quarter of 2024
        >>> df = ibge.ipca_indexes("01-01-2024", "31-03-2024")
    """
    start = convert_input_dates(start)
    end = convert_input_dates(end)

    start_date = start.strftime("%Y%m")
    end_date = end.strftime("%Y%m")
    api_url = f"{IPCA_URL}{start_date}-{end_date}/variaveis/2266?localidades=N1[all]"
    data_dict = _fetch_series_data(api_url)

    return _process_ipca_dataframe(data_dict)

ipca_last_indexes(num_months=1)

Retrieves the last IPCA index values for a specified number of months.

Makes an API call to the IBGE's data portal using the format: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-N/variaveis/2266?localidades=N1[all]

Example: For the last 2 months, the API URL will be: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-2/variaveis/2266?localidades=N1[all]

Parameters:

Name Type Description Default
num_months int

Number of months to retrieve. Defaults to 1.

1

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame with columns 'Period' and 'Value'

Raises:

Type Description
ValueError

If num_months is 0

Examples:

>>> from pyield import ibge
>>> # Get the last month's IPCA index
>>> df = ibge.ipca_last_indexes(1)
>>> # Get the last 3 months' IPCA indexes
>>> df = ibge.ipca_last_indexes(3)
Source code in pyield/ibge/ipca.py
def ipca_last_indexes(num_months: int = 1) -> pd.DataFrame:
    """
    Retrieves the last IPCA index values for a specified number of months.

    Makes an API call to the IBGE's data portal using the format:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-N/variaveis/2266?localidades=N1[all]

    Example: For the last 2 months, the API URL will be:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-2/variaveis/2266?localidades=N1[all]

    Args:
        num_months (int, optional): Number of months to retrieve. Defaults to 1.

    Returns:
        pd.DataFrame: DataFrame with columns 'Period' and 'Value'

    Raises:
        ValueError: If num_months is 0

    Examples:
        >>> from pyield import ibge
        >>> # Get the last month's IPCA index
        >>> df = ibge.ipca_last_indexes(1)
        >>> # Get the last 3 months' IPCA indexes
        >>> df = ibge.ipca_last_indexes(3)
    """
    num_months = abs(num_months)
    if num_months == 0:
        raise ValueError("The number of months must be greater than 0.")

    api_url = f"{IPCA_URL}-{num_months}/variaveis/2266?localidades=N1[all]"
    data_dict = _fetch_series_data(api_url)

    return _process_ipca_dataframe(data_dict)

ipca_last_rates(num_months=1)

Retrieves the last IPCA monthly rates for a specified number of months.

Makes an API call to the IBGE's data portal using the format: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-N/variaveis/63?localidades=N1[all]

Example: For the last 2 months, the API URL will be: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-2/variaveis/63?localidades=N1[all]

Parameters:

Name Type Description Default
num_months int

Number of months to retrieve. Defaults to 1.

1

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame with columns 'Period' and 'Value'

Raises:

Type Description
ValueError

If num_months is 0

Examples:

>>> from pyield import ibge
>>> # Get the last month's IPCA rate
>>> df = ibge.ipca_last_rates(1)
>>> # Get the last 3 months' IPCA rates
>>> df = ibge.ipca_last_rates(3)
Source code in pyield/ibge/ipca.py
def ipca_last_rates(num_months: int = 1) -> pd.DataFrame:
    """
    Retrieves the last IPCA monthly rates for a specified number of months.

    Makes an API call to the IBGE's data portal using the format:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-N/variaveis/63?localidades=N1[all]

    Example: For the last 2 months, the API URL will be:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/-2/variaveis/63?localidades=N1[all]

    Args:
        num_months (int, optional): Number of months to retrieve. Defaults to 1.

    Returns:
        pd.DataFrame: DataFrame with columns 'Period' and 'Value'

    Raises:
        ValueError: If num_months is 0

    Examples:
        >>> from pyield import ibge
        >>> # Get the last month's IPCA rate
        >>> df = ibge.ipca_last_rates(1)
        >>> # Get the last 3 months' IPCA rates
        >>> df = ibge.ipca_last_rates(3)
    """
    num_months = abs(num_months)
    if num_months == 0:
        raise ValueError("The number of months must be greater than 0.")

    api_url = f"{IPCA_URL}-{num_months}/variaveis/63?localidades=N1[all]"
    data_dict = _fetch_series_data(api_url)

    return _process_ipca_dataframe(data_dict, is_in_pct=True)

ipca_rates(start, end)

Retrieves the IPCA monthly rates for a specified date range.

Makes an API call to the IBGE's data portal using the format: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/YYYYMM-YYYYMM/variaveis/63?localidades=N1[all]

Example: For the date range "01-01-2024" to "31-03-2024", the API URL will be: https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/202401-202403/variaveis/63?localidades=N1[all]

Parameters:

Name Type Description Default
start DateScalar

The start date of the date range

required
end DateScalar

The end date of the date range

required

Returns:

Type Description
DataFrame

pd.DataFrame: DataFrame with columns 'Period' and 'Rate'

Examples:

>>> from pyield import ibge
>>> # Get the IPCA rates for the first quarter of 2024
>>> df = ibge.ipca_rates("01-01-2024", "31-03-2024")
Source code in pyield/ibge/ipca.py
def ipca_rates(start: DateScalar, end: DateScalar) -> pd.DataFrame:
    """
    Retrieves the IPCA monthly rates for a specified date range.

    Makes an API call to the IBGE's data portal using the format:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/YYYYMM-YYYYMM/variaveis/63?localidades=N1[all]

    Example: For the date range "01-01-2024" to "31-03-2024", the API URL will be:
    https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/202401-202403/variaveis/63?localidades=N1[all]

    Args:
        start (DateScalar): The start date of the date range
        end (DateScalar): The end date of the date range

    Returns:
        pd.DataFrame: DataFrame with columns 'Period' and 'Rate'

    Examples:
        >>> from pyield import ibge
        >>> # Get the IPCA rates for the first quarter of 2024
        >>> df = ibge.ipca_rates("01-01-2024", "31-03-2024")
    """
    start = convert_input_dates(start)
    end = convert_input_dates(end)

    start_date = start.strftime("%Y%m")
    end_date = end.strftime("%Y%m")
    api_url = f"{IPCA_URL}{start_date}-{end_date}/variaveis/63?localidades=N1[all]"
    data_dict = _fetch_series_data(api_url)

    return _process_ipca_dataframe(data_dict, is_in_pct=True)