Skip to main content

missing_specs()

Build a MissingSpec for each variable with missing values.

Usage

Source

missing_specs()

Each variable that declares missing values (such as the user-defined missing values of an SPSS variable, or the missing-value markers of a Frictionless schema) gets a spec made by its to_missing_spec() method. The dictionary can be given to missing_vals_tbl() as its missing=, and a single spec from it can be given to the missing= parameter of a validation method such as col_vals_regex().

The specs keep the values as they were declared. Frictionless and CSVW markers are strings (like "NA") and are declared for every field, but in a table read with typed columns they can only appear in the string columns. Keep only the specs of those columns before using them, since a string marker can’t be compared with a numeric or date column.

Returns

dict[str, MissingSpec]
A dictionary from column name to MissingSpec. Variables that don’t declare missing values aren’t included.

Examples

The bundled Frictionless Data Package declares "NA" and "N/A" as missing-value markers, so every one of its variables gets a spec:

import pointblank as pb

meta = pb.import_metadata(pb.load_metadata_example("datapackage.json"))
specs = meta.missing_specs()

print(list(specs))
print(specs["customer_id"].reasons)
['transaction_id', 'customer_id', 'amount', 'quantity', 'category', 'sale_date', 'discount_pct', 'email']
{'NA': 'missing_na', 'N/A': 'missing_n_a'}

The reasons were made from the markers, as no labels were declared for them. Let’s keep the specs of the string columns and use one of them in a validation step:

import polars as pl

string_specs = {
    name: spec for name, spec in specs.items() if meta.get_variable(name).dtype == "String"
}

tbl = pl.DataFrame({"customer_id": ["CUST-101", "N/A", "CUST-103", "NA"]})

(
    pb.Validate(data=tbl)
    .col_vals_regex(columns="customer_id", pattern="^CUST-[0-9]+$")
    .col_vals_regex(
        columns="customer_id", pattern="^CUST-[0-9]+$", missing=string_specs["customer_id"]
    )
    .interrogate()
)
STEP COLUMNS VALUES TBL EVAL UNITS PASS FAIL W E C EXT
#4CA64C66 1
col_vals_regex
col_vals_regex()
customer_id ^CUST-[0-9]+$ ✓ 4 2
0.50
2
0.50
— — —
#4CA64C 2
col_vals_regex
col_vals_regex()
customer_id ^CUST-[0-9]+$
MISSING-AWARE
✓ 4 4
1.00
0
0.00
— — — —

Notes

Step 2 (missing_spec) Missing codes: NA→missing_na, N/A→missing_n_a, null→unknown

Without the spec, the "N/A" and "NA" markers fail the pattern check in step 1. With it, step 2 treats them as missing data instead, so all four rows pass. The string_specs dictionary could be given to missing_vals_tbl() in the same way, to summarize the missing data in each string column of a table.