Skip to content

require_columns

require_columns

require_columns(df: DataFrame, columns: list[str], *, what: str) -> None

Raise a didactic SchemaError if df is missing any of columns.

The message names the missing columns, lists the columns that were present, gives the likely cause, and points to a concrete fix, so the most common user error (passing a frame that did not come from a to_canonical_* normaliser) explains itself.

Source code in src/gbfs_toolkit/core/models.py
def require_columns(df: pd.DataFrame, columns: list[str], *, what: str) -> None:
    """Raise a didactic [`SchemaError`][gbfs_toolkit.SchemaError] if ``df`` is missing any of ``columns``.

    The message names the missing columns, lists the columns that were present, gives the
    likely cause, and points to a concrete fix, so the most common user error (passing a
    frame that did not come from a ``to_canonical_*`` normaliser) explains itself.
    """
    missing = [c for c in columns if c not in df.columns]
    if not missing:
        return
    present = list(df.columns)
    message = (
        f"{what}: missing required column(s) {missing}.\n"
        f"  present columns: {present}\n"
        f"  likely cause: this frame did not come from a to_canonical_* normaliser, or an "
        f"optional GBFS field is absent for this system.\n"
        f"  fix: normalise the raw feed with the matching to_canonical_* function, or add the "
        f"column(s) before calling. Canonical schema: {_DATA_MODEL_URL}"
    )
    raise SchemaError(message, missing=missing, present=present, what=what)