SGS

A função bcb.sgs.get() obtem os dados do webservice do Banco Central , interface json do serviço BCData/SGS - Sistema Gerenciador de Séries Temporais (SGS).

Exemplos

In [1]: from bcb import sgs

In [2]: import matplotlib.pyplot as plt

In [3]: import matplotlib as mpl

In [4]: mpl.style.use('bmh')

In [5]: df = sgs.get({'IPCA': 433}, start='2002-02-01')

In [6]: df.index = df.index.to_period('M')

In [7]: df.head()
Out[7]: 
         IPCA
Date         
2002-02  0.36
2002-03  0.60
2002-04  0.80
2002-05  0.21
2002-06  0.42
In [8]: dfr = df.rolling(12)

In [9]: i12 = dfr.apply(lambda x: (1 + x/100).prod() - 1).dropna() * 100

In [10]: i12.head()
Out[10]: 
              IPCA
Date              
2003-01  14.467041
2003-02  15.847124
2003-03  16.572608
2003-04  16.769209
2003-05  17.235307
In [11]: i12.plot(figsize=(12,6))
Out[11]: <Axes: xlabel='Date'>

In [12]: plt.title('Fonte: https://dadosabertos.bcb.gov.br', fontsize=10)
Out[12]: Text(0.5, 1.0, 'Fonte: https://dadosabertos.bcb.gov.br')

In [13]: plt.suptitle('IPCA acumulado 12 meses - Janela Móvel', fontsize=18)
Out[13]: Text(0.5, 0.98, 'IPCA acumulado 12 meses - Janela Móvel')

In [14]: plt.xlabel('Data')
Out[14]: Text(0.5, 0, 'Data')

In [15]: plt.ylabel('%')
Out[15]: Text(0, 0.5, '%')

In [16]: plt.legend().set_visible(False)
_images/sgs1.png

Obtendo o JSON bruto

A função bcb.sgs.get_json() retorna o JSON bruto da API para um único código. Para pipelines de dados onde o dado bruto deve ser persistido antes de qualquer transformação, o parâmetro output='text' pode ser passado à função bcb.sgs.get().

Para um único código é retornada uma str; para múltiplos códigos é retornado um dict mapeando código inteiro → JSON string.

from bcb import sgs

# único código → str
raw = sgs.get(433, start='2024-01-01', output='text')

# múltiplos códigos → dict[int, str]
raws = sgs.get([433, 189], start='2024-01-01', output='text')
# raws[433] → JSON string do IPCA
# raws[189] → JSON string do IGP-M

# salvar em disco
with open('ipca_raw.json', 'w') as f:
    f.write(raw)

O JSON retornado é um array de objetos com os campos data e valor, exatamente como devolvido pela API BCData/SGS. O comportamento padrão (retorno de DataFrame) é mantido quando o parâmetro não é informado.

Dados de Inadimplência de Operações de Crédito

In [17]: from bcb.sgs.regional_economy import get_non_performing_loans

In [18]: from bcb.utils import BRAZILIAN_REGIONS, BRAZILIAN_STATES

In [19]: import pandas as pd

In [20]: get_non_performing_loans(["RR"], last=10, mode="all")
Out[20]: 
              RR
Date            
2025-03-01  4.20
2025-04-01  4.46
2025-05-01  4.59
2025-06-01  4.77
2025-07-01  5.06
2025-08-01  5.21
2025-09-01  5.07
2025-10-01  5.28
2025-11-01  5.28
2025-12-01  5.19
In [21]: northeast_states = BRAZILIAN_REGIONS["NE"]

In [22]: get_non_performing_loans(northeast_states, last=5, mode="pj")
Out[22]: 
              AL    BA    CE    MA    PB    PE    PI    RN    SE
Date                                                            
2025-08-01  2.41  3.09  3.74  6.41  5.14  3.39  2.69  4.50  5.32
2025-09-01  2.58  3.07  3.62  6.03  4.89  3.29  2.45  4.48  4.82
2025-10-01  2.82  3.14  3.77  6.31  8.56  3.31  2.37  4.65  4.78
2025-11-01  2.85  3.10  3.73  6.11  8.40  3.17  2.24  4.25  4.70
2025-12-01  2.67  2.92  3.46  5.74  8.32  2.97  2.11  3.83  4.36
In [23]: get_non_performing_loans(BRAZILIAN_STATES, mode="PF", start="2024-01-01")
Out[23]: 
              AC    AP    AM    PA    RO  ...    RJ    SP    PR    RS    SC
Date                                      ...                              
2024-01-01  3.49  4.09  5.40  4.16  2.76  ...  5.33  3.45  2.76  2.48  2.85
2024-02-01  3.48  4.05  5.25  4.15  2.78  ...  5.25  3.43  2.75  2.51  2.84
2024-03-01  3.43  4.01  5.18  4.09  2.81  ...  5.17  3.36  2.74  2.53  2.81
2024-04-01  3.46  4.10  5.15  4.09  2.85  ...  5.13  3.41  2.75  2.53  2.81
2024-05-01  3.54  4.22  5.26  4.15  2.99  ...  5.14  3.46  2.83  2.60  2.87
2024-06-01  3.50  4.11  5.14  4.09  3.07  ...  5.04  3.40  2.76  2.61  2.79
2024-07-01  3.49  4.13  5.14  4.14  3.16  ...  5.02  3.43  2.87  2.61  2.82
2024-08-01  3.41  4.03  5.05  4.13  3.23  ...  4.97  3.42  3.02  2.58  2.81
2024-09-01  3.55  4.06  4.99  4.16  3.24  ...  4.90  3.39  3.01  2.54  2.79
2024-10-01  3.55  3.98  4.86  4.21  3.26  ...  4.84  3.35  2.97  2.49  2.74
2024-11-01  3.49  3.95  4.74  4.23  3.30  ...  4.80  3.32  2.94  2.41  2.70
2024-12-01  3.52  4.02  4.64  4.20  3.34  ...  4.70  3.28  2.87  2.29  2.66
2025-01-01  3.86  4.42  4.91  4.48  3.67  ...  5.08  3.56  3.11  2.51  2.87
2025-02-01  3.95  4.50  5.04  4.61  3.85  ...  5.13  3.64  3.22  2.73  2.99
2025-03-01  3.84  4.57  4.91  4.60  3.86  ...  5.10  3.64  3.22  2.90  3.03
2025-04-01  4.08  4.90  5.17  4.82  4.18  ...  5.30  3.80  3.47  3.18  3.26
2025-05-01  4.19  5.03  5.35  4.96  4.27  ...  5.46  3.90  3.54  3.37  3.39
2025-06-01  4.30  5.19  5.44  5.03  4.23  ...  5.45  3.91  3.51  3.44  3.43
2025-07-01  4.51  5.33  5.69  5.26  4.69  ...  5.58  4.07  3.76  3.66  3.58
2025-08-01  4.73  5.39  5.82  5.45  5.03  ...  5.67  4.19  4.08  3.94  3.71
2025-09-01  4.64  5.34  5.83  5.46  5.12  ...  5.61  4.13  4.05  4.15  3.60
2025-10-01  4.91  5.54  5.95  5.84  5.52  ...  5.74  4.24  4.18  4.58  3.74
2025-11-01  4.97  5.52  5.95  5.89  5.79  ...  5.73  4.26  4.28  4.68  3.78
2025-12-01  5.07  5.67  6.05  5.86  5.76  ...  5.71  4.21  4.15  4.65  3.77

[24 rows x 27 columns]