This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
python-bcb is a Python interface to the Brazilian Central Bank (Banco Central do Brasil) open data APIs. It provides access to time series data, exchange rates, market expectations, and financial institution data.
This project uses uv exclusively. All Python commands must be run via uv run. Never invoke python, pytest, ruff, or any other Python tool directly.
uv run python ...
uv run pytest ...
uv run ruff ...curl -LsSf https://astral.sh/uv/install.sh | sh # install uv
uv sync # install all dependency groups
uv sync --group test # install with test dependencies onlyuv run pytest # run all tests
uv run pytest tests/test_utils.py # run a single test file
uv run pytest tests/test_currency.py::test_currency_id # run a single testNote: Many tests make live HTTP requests to BCB APIs. Tests marked with @mark.flaky may fail intermittently due to network issues or API availability.
uv run ruff check bcb/ # lint with ruff
uv run ruff format bcb/ # format with ruffcd docs && uv run sphinx-build -b html . _build/htmlThe package is organized into three main API modules under bcb/:
Fetches time series from the BCB's SGS (Sistema Gerenciador de Séries Temporais) JSON API. The main entry point is sgs.get(codes, start, end, last, multi, freq). Codes can be int, list, tuple, or dict for named series. Returns pandas DataFrames with DatetimeIndex or PeriodIndex.
bcb/sgs/__init__.py—get()andget_json()functions +SGSCodeclassbcb/sgs/regional_economy.py— wrapper for regional non-performing loan series with pre-mapped SGS codes by state/region
Scrapes the BCB PTAX website for daily bid/ask exchange rates. Uses httpx + lxml for HTML parsing.
bcb/currency.py—get(symbols, start, end, side, groupby)returns multi-indexed DataFrames. Module-level_CACHEdict avoids redundant HTTP requests within a session.
A generic OData client plus named wrappers for specific BCB OData services (hosted at olinda.bcb.gov.br).
bcb/odata/framework.py— Core OData machinery:ODataServicefetches and parses the$metadataXML document (vialxml) to discover entity sets, functions, and properties.ODataQuerybuilds and executes queries with chainable methods (.filter(),.orderby(),.select(),.limit(),.skip()).ODataPropertyinstances support Python comparison operators to createODataPropertyFilterobjects.bcb/odata/api.py—BaseODataAPIbase class and concrete named classes (Expectativas,PTAX,IFDATA,TaxaJuros,MercadoImobiliario,SPI, etc.). Each subclass just setsBASE_URL. TheEndpointclass (withEndpointMetametaclass) exposes entity properties as attributes, enablingendpoint.PropertyName >= valuefilter syntax.EndpointQuery.collect()post-processes date columns intopd.Timestamp.bcb/__init__.py— re-exports all OData API classes at the top level (e.g.,from bcb import PTAX).
Dateclass: normalizesstr,datetime,dateinputs. Accepts"today"/"now"strings.BRAZILIAN_REGIONS/BRAZILIAN_STATESconstants for geographic lookups.DateInputtype alias used throughout the codebase.
Every task must pass all of the following before it is considered complete:
uv run pytest -m "not integration" # all unit tests pass
uv run ruff check bcb/ tests/ # no lint errors
uv run ruff format --check bcb/ tests/ # code is formatted
uv run mypy bcb/ # no type errors- OData query building:
api.get_endpoint("EntityName")returns anEndpoint. Call.get(Property >= value, limit=100)for one-shot queries, or.query()for a chainableEndpointQuery. ODataAPIfor unlisted services: Pass any valid OData URL directly toODataAPI(url)to access BCB APIs not yet wrapped.- All network calls use
httpx(sync) withfollow_redirects=True. The OData metadata fetch happens lazily on firstBaseODataAPIinstantiation. @mark.flaky: flaky tests use theflakylibrary withmax_runsretries to handle transient API failures.