imibare

The statistics East African governments publish, gathered into one register and kept exactly as they were published.

IMIBARE · DOCS · REFERENCE

Python package

Reference for the imibare Python package: loading data, browsing metadata, versions, environment variables, and the local cache.

Use the Python package when you want an imibare dataset as a DataFrame. We keep the interface small: load() reads data and catalog() tells you what data exists.

Install and import

Install the base package for pandas support. Add an extra only when you need Polars or private Iceberg history access.

If you want toInstall
pandas DataFrames and the bundled catalogpip install imibare
Polars DataFramespip install 'imibare[polars]'
Historical Iceberg snapshotspip install 'imibare[iceberg]'
import imibare as imi

Load data

Call imi.load() with a 4-segment dataset ID. It returns the latest published data through the public API unless you explicitly request a historical version.

def load(
    dataset_id: str,
    start: int | str | None = None,
    end: int | str | None = None,
    engine: Literal["pandas", "polars"] = "pandas",
    version: str | None = None,
    force_download: bool = False,
) -> pd.DataFrame | pl.DataFrame

Choose the arguments

ArgumentDefaultUse it whenWhat we do
dataset_idrequiredYou know the dataset to loadValidate the 4-segment ID and retrieve that dataset
startNoneYou want rows on or after a year or ISO dateTreat an integer such as 2020 as 2020-01-01
endNoneYou want rows on or before a year or ISO dateApply an inclusive upper bound
engine"pandas"Your analysis uses PolarsReturn pl.DataFrame when you pass "polars"
versionNoneYou have R2 Data Catalog access and need historyRead the snapshot published on or before YYYY-MM-DD
force_downloadFalseYou need to bypass a local cacheDownload the latest API file without revalidating the cache

start and end accept an integer year, an ISO date string, or None. Both bounds are inclusive, so start=2020, end=2023 keeps the complete 2020 through 2023 period.

Get the result

load() returns a pandas DataFrame by default and a Polars DataFrame when engine="polars". Its columns match the dataset record from catalog() and GET /v1/datasets/{id}, with dates parsed as datetime columns.

Use the public path unless you have a reason not to. A version request reads our Iceberg catalog directly and needs the [iceberg] extra plus credentials that we do not issue publicly.

Handle errors and warnings

What happensWhat you receiveWhat to do
The ID is malformed, unknown, or has no snapshot before versionValueErrorCheck the dataset ID and requested date
The ID exists but no data has been publishedFileNotFoundErrorCheck the dataset page or wait for the pipeline to publish
The API fails and there is no usable cacheRuntimeErrorRetry later or load the dataset before you need offline access
Polars or Iceberg support is missingImportErrorInstall the matching extra
The API fails but a cache existsUserWarning and cached dataRead the warning, which states when the file was last confirmed current

Examples

import imibare as imi

# Latest complete dataset
cpi = imi.load("rw.nisr.cpi.monthly")

# Kenya uses the same interface
kenya_cpi = imi.load("ke.knbs.cpi.monthly")

# Inclusive year or exact-date bounds
recent_cpi = imi.load("rw.nisr.cpi.monthly", start=2020, end=2023)
usd_2024 = imi.load("rw.bnr.fx.daily", start="2024-01-01", end="2024-12-31")

# A Polars DataFrame
polars_cpi = imi.load("rw.nisr.cpi.monthly", engine="polars")

# The newest file, without using the cache
fresh_cpi = imi.load("rw.nisr.cpi.monthly", force_download=True)

Browse the catalog

Call imi.catalog() when you need IDs or want to inspect coverage before loading. The catalog ships with the package, so it works offline and represents the data known when your installed package version was released.

def catalog(
    country: str | list[str] | None = None,
    topic: str | None = None,
    frequency: str | None = None,
) -> list[DatasetMeta]

Filter the records

ArgumentAccepted valuesWhat it matches
countryAn ISO alpha-2 code, a list of codes, or NoneCountry, case-insensitively: "RW", "KE", or ["RW", "KE"]
topicA metadata topic or NoneExact metadata topic, such as "prices"
frequencyA frequency slug or Nonedaily, monthly, quarterly, annual, or irregular

The metadata topic is not always the third part of the ID. For example, rw.nisr.cpi.monthly has the metadata topic prices, so use a topic you have seen in a catalog record rather than guessing from the ID.

# All bundled records
all_datasets = imi.catalog()

# Country and frequency filters
kenya = imi.catalog(country="KE")
monthly = imi.catalog(country="KE", frequency="monthly")

# Consumer price and inflation datasets in both countries
prices = imi.catalog(topic="prices")

for dataset in imi.catalog(country="RW"):
    print(dataset.id, dataset.name)

catalog() returns an empty list when nothing matches. Upgrade the package to see datasets that were added after your installed release.

Read dataset metadata

Each DatasetMeta is a record from the bundled catalog. It describes the publisher, coverage, schema, formats, and reuse information, but leaves the live last_updated and version empty because those values can change after the package ships.

@dataclass
class DatasetMeta:
    id: str
    country: str
    country_name: str
    name: str
    institution: str
    institution_slug: str
    topic: str
    frequency: str
    coverage_start: str
    coverage_end: str
    pipeline: Literal["automated", "curated", "stale"]
    source_url: str
    source_format: str
    formats: list[str]
    license: str
    license_basis: str
    columns: list[ColumnSpec]
    last_updated: str = ""
    version: str = ""
    notes: str | None = None
    license_url: str | None = None

Use the API record when you need the live version, update date, or version history. The license field records what the publisher states; it is not a license granted by imibare.

@dataclass
class ColumnSpec:
    name: str
    type: str
    description: str
    unit: str | None

Check the installed version

imibare.__version__

The value is a string, such as "0.3.0". It tells you which bundled catalog and package behavior you have installed.

Configure the package

You normally need no configuration. Set these variables only when you use a different API host or private Iceberg history access.

VariableNeeded forWhy
IMIBARE_API_URLA mirror or local APIReplaces the default https://api.imibare.org host
R2_ACCOUNT_IDversion=Identifies the Cloudflare account that owns the catalog
R2_BUCKETversion=Names the R2 bucket
R2_CATALOG_URLversion=Points to the Iceberg REST catalog
R2_CATALOG_TOKENversion=Authenticates to the Iceberg catalog

Understand the cache

We keep the latest file for each dataset at this path, next to a latest.etag file that records the API version we received. The cache makes repeated analysis quicker while still checking for changes on a predictable schedule.

~/.imibare/cache/{country}/{institution}/{topic}/{frequency}/latest.parquet
Cache stateWhat load() does
File is less than 1 hour oldUses it with no request
File is older than 1 hour and unchangedSends its ETag, receives 304 Not Modified, and keeps it for another hour
File is older than 1 hour and changedDownloads the replacement
API is unavailable and a file existsReturns the file with a UserWarning
force_download=TrueSkips this process and downloads the latest file

Delete ~/.imibare/cache/ if you need to clear every cached dataset. Removing the directory is safe, but the next call for each dataset will need to download it again.