BacenData
Acesso simplificado aos dados do Banco Central do Brasil.
Resolve automaticamente a limitacao de 10 anos por consulta imposta pelo BACEN em marco/2025, com paginacao transparente, retry inteligente e cache local.
Instalacao
pip install bacendata
Uso rapido
from bacendata import sgs
# Buscar a taxa Selic (por codigo ou nome)
selic = sgs.get(11, start="2020-01-01")
selic = sgs.get("selic", start="2020-01-01")
# Buscar multiplas series de uma vez
df = sgs.get({"Selic": 11, "IPCA": 433, "Dolar": 1}, start="2010-01-01")
# Ultimos N valores
ipca = sgs.get(433, last=12)
# Buscar mais de 10 anos (paginacao automatica!)
selic_longa = sgs.get(11, start="2000-01-01", end="2024-12-31")
Funcionalidades
- Paginacao automatica — consultas >10 anos sao divididas em chunks e feitas em paralelo
- Multiplas series — busque varias series de uma vez com
dict - Catalogo integrado — use nomes em vez de codigos:
sgs.get("selic") - Cache local — evite chamadas repetidas a API do BACEN
- Retry com backoff — trata automaticamente erros 429, 500 e timeouts
- Formato flexivel — aceita datas ISO (
2024-01-01), brasileiro (01/01/2024) e objetosdate - Retorno pandas — dados prontos para analise como
DataFrame - Async nativo — interface sync (
get) e async (aget) para FastAPI
Catalogo de series
Voce pode usar nomes em vez de codigos numericos:
sgs.get("selic") # Taxa Selic diaria (codigo 11)
sgs.get("ipca") # IPCA mensal (codigo 433)
sgs.get("dolar") # Dolar PTAX compra (codigo 1)
sgs.get("euro") # Euro compra PTAX (codigo 21619)
sgs.get("euro") # Euro compra (codigo 10813)
sgs.get("focus_ipca") # Expectativa IPCA 12m (codigo 27574)
sgs.get("focus_selic") # Expectativa Selic (codigo 27575)
sgs.get("inadimplencia_pf") # Inadimplencia PF (codigo 21112)
Para listar todas as series disponiveis:
from bacendata.wrapper.catalogo import listar
for serie in listar():
print(f"{serie.codigo:>6} | {serie.nome} ({serie.periodicidade})")
Cache local
Ative o cache para evitar chamadas repetidas:
from bacendata import sgs
sgs.cache.ativar() # Salva em ~/.bacendata/cache.db
selic = sgs.get(11, start="2020-01-01") # Primeira vez: busca na API
selic = sgs.get(11, start="2020-01-01") # Segunda vez: le do cache
# Caminho customizado
sgs.cache.ativar("/tmp/meu_cache.db")
# Limpar cache
sgs.cache.limpar()
# Desativar
sgs.cache.desativar()
TTL padrao por periodicidade:
- Series diarias: 1 hora
- Series semanais: 6 horas
- Series mensais: 24 horas
Metadados
info = sgs.metadata(433)
# {'codigo': 433, 'nome': 'IPCA - Variacao mensal', 'periodicidade': 'Mensal', ...}
Interface async
Para uso em FastAPI ou outros frameworks async:
import asyncio
from bacendata import sgs
async def main():
df = await sgs.aget(11, start="2020-01-01")
print(df)
asyncio.run(main())
Series disponiveis no catalogo
| Codigo | Nome | Periodicidade | Aliases |
|---|---|---|---|
| 1 | Dolar (compra) | Diaria | dolar, usd, ptax, cambio |
| 11 | Selic diaria | Diaria | selic, selic_diaria |
| 12 | Selic acumulada no mes | Mensal | selic_mensal, selic_acumulada |
| 433 | IPCA | Mensal | ipca, inflacao |
| 4189 | Juros PF | Mensal | juros_pf, taxa_pf |
| 4390 | Selic acumulada anualizada | Mensal | selic_anual, selic_anualizada |
| 7326 | Reservas Internacionais | Diaria | reservas |
| 21619 | Euro (compra) PTAX | Diaria | euro, eur |
| 10813 | Euro (compra) | Diaria | euro, eur |
| 20542 | Saldo Credito Livre | Mensal | saldo_credito, carteira_credito |
| 21082 | Inadimplencia PJ | Mensal | inadimplencia_pj, default_pj |
| 21112 | Inadimplencia PF | Mensal | inadimplencia_pf, default_pf |
| 25434 | Juros Credito Livre | Mensal | juros_credito, credito_livre |
| 27574 | Expectativa IPCA 12m | Semanal | focus_ipca, expectativa_ipca |
| 27575 | Expectativa Selic | Semanal | focus_selic, expectativa_selic |
Qualquer codigo SGS funciona mesmo fora do catalogo: sgs.get(99999, last=10).
Tratamento de erros
from bacendata.wrapper.exceptions import (
SerieNaoEncontrada,
BacenAPIError,
BacenTimeoutError,
ParametrosInvalidos,
)
try:
df = sgs.get(99999, start="2024-01-01")
except SerieNaoEncontrada:
print("Serie nao existe")
except BacenTimeoutError:
print("API do BACEN nao respondeu")
except BacenAPIError as e:
print(f"Erro {e.status_code}: {e.mensagem}")
Desenvolvimento
git clone https://github.com/fmaignacio/bacendata.git
cd bacendata
python -m venv .venv
source .venv/bin/activate # Linux/Mac
pip install -e ".[dev]"
# Testes
python -m pytest tests/ -v --cov=src/bacendata
# Lint
ruff check src/ tests/
black src/ tests/
Licenca
MIT
Metadata
Release files for bacendata 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| bacendata-0.2.0.tar.gz | 35.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bacendata-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 67.3 kB
Release files / bacendata-0.2.0.tar.gz
| Download URL | bacendata-0.2.0.tar.gz |
|---|---|
| Size | 35.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6b22ee6806644fda976d480ca4eb5b986d5edea31a7e9bec71b6f8cfc283ee04
|
|
BLAKE2b-256 checksum How to use checksums |
16416b13d5d5bd589bfbe42bee301e4f28c6f4180654cfd22ac5bff5b5707cf5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 27, 2026.
Transparency logRelease files / bacendata-0.2.0-py3-none-any.whl
| Download URL | bacendata-0.2.0-py3-none-any.whl |
|---|---|
| Size | 32.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
443021517f74b76df7ebaef706c5687dcd613c38a68b8b25258cf7ff79da07d4
|
|
BLAKE2b-256 checksum How to use checksums |
ddf4569a408173efca20aea349e2770857ba172af6ec5efa900ad1688a66660b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Feb 27, 2026.
Transparency log