Skip to content

Fix currency.get() by migrating from discontinued BCB CSV to PTAX OData API#28

Open
Copilot wants to merge 2 commits intomainfrom
copilot/fix-erro-obter-dados-moedas
Open

Fix currency.get() by migrating from discontinued BCB CSV to PTAX OData API#28
Copilot wants to merge 2 commits intomainfrom
copilot/fix-erro-obter-dados-moedas

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 24, 2026

currency.get() was broken because http://www4.bcb.gov.br/Download/fechamento/M{date}.csv now returns an HTML "Requisição inválida!" error page (HTTP 200) instead of CSV, causing pandas.errors.ParserError on every call.

Changes

bcb/currency.py

Replaced the two-step legacy approach (HTML form scrape for numeric IDs + broken CSV for symbol mapping) with direct calls to the official BCB PTAX OData API at olinda.bcb.gov.br:

Removed Replacement
_get_valid_currency_list() — broken www4.bcb.gov.br CSV get_currency_list()/Moedas?$format=json
_currency_id_list() — HTML <select> scraper for numeric IDs _validate_currency_symbol() — checks symbol against OData list
_get_currency_id() — merged both above to get symbol → numeric ID (not needed; OData accepts symbol directly)
_currency_url() — numeric-ID-based HTML endpoint URL _currency_url()CotacaoMoedaPeriodo(moeda=@moeda,...) OData function
_get_symbol() — parsed semicolon-delimited CSV _get_symbol() — parses OData JSON, filters tipoBoletim == "Fechamento" for closing rates

Removed now-unused imports: re, warnings, date, timedelta, BytesIO, StringIO, numpy, lxml.html.

get_currency_list() column schema changes: previously returned code, name, symbol, country_code, country_name, type, exclusion_date (from CSV); now returns symbol, name, type (from OData Moedas endpoint).

tests/conftest.py + tests/test_currency.py

Replaced HTML/CSV mock fixtures with OData JSON mocks. Tests for removed helpers (_currency_id_list, _get_currency_id) replaced with tests for _validate_currency_symbol.

Example

from bcb import currency
# Previously raised pandas.errors.ParserError due to HTML response on CSV endpoint
df = currency.get(symbols='USD', start='2025-10-06', end='2025-10-09')

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • astral.sh
    • Triggering command: /usr/bin/curl curl -LsSf REDACTED (dns block)
  • releases.astral.sh
    • Triggering command: /home/REDACTED/.local/bin/uv uv sync (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>erro ao obter dados de moedas usando o metodo currency.get</issue_title>
<issue_description>Erro ao obter dados de moedas. por exemplo:

from bcb import currency
currency.get(symbols='USD', start='2025-10-06', end='2025-10-09')

http://www4.bcb.gov.br/Download/fechamento/M20260323.csv
<?xml version="1.0" encoding="pt-br"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pt-br" xml:lang="pt-br">
<head>
<title>Requisição inválida!</title>
<link rev="made" />
<style>

....

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/bcb/currency.py", line 173, in get
    df1 = _get_symbol(symbol, start, end)
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/bcb/currency.py", line 107, in _get_symbol
    cid = _get_currency_id(symbol)
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/bcb/currency.py", line 101, in _get_currency_id
    all_currencies = get_currency_list()
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/bcb/currency.py", line 80, in get_currency_list
    df = pd.read_csv(StringIO(res.text), delimiter=";")
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1026, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 626, in _read
    return parser.read(nrows)
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1923, in read
    ) = self._engine.read(  # type: ignore[attr-defined]
  File "/home/ubuntu/extracao/extracao-env/lib/python3.10/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 234, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas/_libs/parsers.pyx", line 838, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas/_libs/parsers.pyx", line 905, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 874, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas/_libs/parsers.pyx", line 891, in pandas._libs.parsers.TextReader._check_tokenize_status
  File "pandas/_libs/parsers.pyx", line 2061, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 9, saw 3

Aparentemente a url que ele usa para baixar os dados está inválida. Talvez tenha sido descontinuada.

python == 3.10
python-bcb == 0.3.3
ubuntu == 22.04
</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wilsonfreitas wilsonfreitas marked this pull request as ready for review March 24, 2026 13:28
@wilsonfreitas wilsonfreitas requested a review from Copilot March 24, 2026 13:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI changed the title [WIP] Fix error when fetching currency data using currency.get Fix currency.get() by migrating from discontinued BCB CSV to PTAX OData API Mar 24, 2026
Copilot AI requested a review from wilsonfreitas March 24, 2026 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

erro ao obter dados de moedas usando o metodo currency.get

3 participants