Skip to main content

opendata-rfb

Receita Federal emblem

Project Status: Active Python Version PyPI Version Docs Linting Type-checked: mypy Test Coverage License Open Issues Contributions Welcome

Overview

opendata-rfb reads the Receita Federal do Brasil (RFB) CNPJ open data (the company registry) into typed pandas DataFrames. It covers every dataset the RFB publishes:

Dataset Reader Content
Empresas read_companies Legal entities (basic CNPJ, name, legal nature, share capital)
Estabelecimentos read_establishments Establishments, with the full 14-character CNPJ and its validity
Socios read_partners Partners and representatives (CPFs stay masked)
Simples read_simples Simples Nacional and MEI options
Cnaes, Motivos, Municipios, Naturezas, Paises, Qualificacoes read_lookup Code lookup tables

Data source and cadence. The data comes from the RFB's public share (arquivos.receitafederal.gov.br), which publishes one folder per reference month (YYYY-MM), refreshed monthly. A reader takes the month as its argument; a month the share does not publish raises an error rather than falling back to another one.

⚠️ Size. The archives are large and are never held in memory: each reader streams every ZIP to disk and yields typed chunks. Estabelecimentos is the heaviest (ten parts, up to 2.3 GB each compressed) and Simples.zip inflates to ~3.2 GB. Plan for disk space and a long first run; pass path_workdir= to keep the ZIPs so a rerun resumes instead of downloading again.

Documentation

The full documentation is published at https://guilhermegor.github.io/opendata-rfb/:

To serve it locally: poetry run poe docs_serve.

Getting Started

Python 3.10+ is the only prerequisite.

Install

pip install opendata-rfb
pip install 'opendata-rfb[parquet]'   # optional: Parquet export (pulls in pyarrow)

Or with Poetry:

poetry add opendata-rfb
poetry add 'opendata-rfb[parquet]'   # optional: Parquet export (pulls in pyarrow)

To work on the library itself, see CONTRIBUTING.md.

Usage

Every dataset reader (read_companies, read_establishments, read_partners, read_simples) takes a reference month (YYYY-MM) and yields chunks of int_chunk_rows rows (default 500,000), so memory stays flat. read_lookup is the exception: it takes str_table, path_zip, str_url, str_etag and returns a single DataFrame, since each lookup table is small. Every row carries provenance columns (url, updated_at, source_key, package_version, ingestion_run_id, content_hash, source_etag).

Empresas. capital_social is an exact Decimal, never a float.

from opendata_rfb import read_companies

for df_chunk in read_companies("2026-10"):
    ...

Estabelecimentos. Adds the full cnpj_completo and its validity flag cnpj_valido (alphanumeric-aware).

from opendata_rfb import read_establishments

for df_chunk in read_establishments("2026-10"):
    ...

Socios. Masked CPFs (***240659**) are kept verbatim, never unmasked; ***000000** as representative document becomes null.

from opendata_rfb import read_partners

for df_chunk in read_partners("2026-10"):
    ...

Simples. Booleans and real dates, with the RFB's 00000000 read as null.

from opendata_rfb import read_simples

for df_chunk in read_simples("2026-10"):
    ...

Lookups. read_lookup reads one downloaded lookup ZIP (one of CNPJ_LOOKUP_NAMES).

from pathlib import Path

from opendata_rfb import read_lookup
from opendata_rfb._internal.utils.stream_downloader import download_file_streaming
from opendata_rfb.ingestion.cnpj._share import file_url, latest_reference_month

str_url = file_url(latest_reference_month(), "Cnaes.zip")
path_zip, str_etag = download_file_streaming(str_url, Path("data/raw/Cnaes.zip"))
df_cnaes = read_lookup("Cnaes", path_zip, str_url, str_etag)

Parquet. export_companies_parquet("2026-10", path) writes Empresas to one Parquet file for lazy querying (DuckDB, Polars scan_parquet); it needs the parquet extra.

⚠️ Do not read these files with Polars directly: it silently misparses the .zip and corrupts latin-1 text.

Project Structure

opendata-rfb/
├── .github/              workflows, CODEOWNERS, PR template
├── assets/               logo
├── bin/                  gate scripts and helpers
├── docs/                 published documentation (mkdocs)
├── src/opendata_rfb/
│   ├── __init__.py       the public API (__all__)
│   ├── ingestion/cnpj/   one reader per RFB dataset
│   └── _internal/        private: contracts, utils, ports
├── tests/
│   ├── unit/
│   └── integration/
├── pyproject.toml
└── poe_tasks.toml        task runner entry points

Development

git clone https://github.com/guilhermegor/opendata-rfb.git
cd opendata-rfb
bash bin/venv.sh                           # first time only: creates .venv; poe exists only after this
poetry run poe init                        # venv, git hooks, repo rules, security, Pages
poetry run poe enable_pages                # maintainers, one time, after the first PyPI release

If bin/venv.sh just installed Poetry into ~/.local/bin, add that directory to PATH before poetry run works. init also runs GitHub helpers that only maintainers can use; on a fork they warn and continue. enable_pages is already done for this repo.

Daily commands:

poetry run poe unit_tests                  # unit suite
poetry run poe integration_tests           # integration suite
poetry run poe lint                        # ruff, codespell and the structure gates
poetry run poe docs_serve                  # serve the docs locally

Public API

The public surface is whatever the package __init__.py lists in __all__; everything under _internal/ is private.

  • read_companies(str_month, int_chunk_rows=500_000, path_workdir=None): Empresas chunks.
  • read_establishments(...): Estabelecimentos chunks, same signature.
  • read_partners(...): Socios chunks, same signature.
  • read_simples(...): Simples chunks, same signature.
  • export_companies_parquet(str_month, path_dest, int_chunk_rows=500_000, path_workdir=None): Empresas to one Parquet file (needs the parquet extra).
  • read_lookup(str_table, path_zip, str_url, str_etag): one of the six lookup tables (CNPJ_LOOKUP_NAMES) as a typed DataFrame.
  • CNPJ_LOOKUP_NAMES, __version__.

Publishing

Releases go out from the release workflows under .github/workflows/ through PyPI trusted publishing (OIDC); the git tag is the version.

Contributing

See CONTRIBUTING.md for the branch, commit and review rules.

License

This project is licensed under MIT; see LICENSE.

Authors


Generated from the lib-minimal template via BlueprintX. Unofficial, community library. Not affiliated with or endorsed by the Receita Federal do Brasil; the emblem is theirs.

Metadata

Release files for opendata-rfb 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opendata-rfb 2.0.0
File Size Uploaded Provenance Actions
opendata_rfb-2.0.0.tar.gz 81.9 kB With provenance Details for opendata_rfb-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opendata-rfb 2.0.0
File Interpreter ABI Platform Actions
opendata_rfb-2.0.0-py3-none-any.whl Python 3 none any Details for opendata_rfb-2.0.0-py3-none-any.whl

Total release size: 192.1 kB

Release files / opendata_rfb-2.0.0.tar.gz

Download URL opendata_rfb-2.0.0.tar.gz
Size 81.9 kB
Tags Source
SHA-256 checksum
How to use checksums
34df7c9498b35796cbd864bab2c70e6fec10e675a0e5ee0ada9894951a559838
BLAKE2b-256 checksum
How to use checksums
481a9e32789bd590cb84b83760197c6ae912693a8294f310333f150e501270c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 12, 2026.

Transparency log

Release files / opendata_rfb-2.0.0-py3-none-any.whl

Download URL opendata_rfb-2.0.0-py3-none-any.whl
Size 110.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cd05d4092a39f336153270b1a75729cdd5bde37a4929634ab85709f9112842c9
BLAKE2b-256 checksum
How to use checksums
f4b0c27e6e363751d10b748224cf3a81772983dfb11927174c1167e5c77a85e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 12, 2026.

Transparency log

Security for opendata-rfb 2.0.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for opendata_rfb-2.0.0.tar.gz

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 Nov 12, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page