Complete worked example
python
import pandas as pd
def inspect_example(roles, companies):
left = pd.DataFrame(roles, columns=["id", "company_id"]).astype("string")
right = pd.DataFrame(companies, columns=["company_id", "name"]).astype("string")
if left["id"].isna().any() or left["id"].duplicated().any() or left["company_id"].isna().any() or right["company_id"].isna().any() or right["company_id"].duplicated().any():
raise ValueError("ambiguous or missing identity")
joined = left.merge(right, on="company_id", how="left", validate="many_to_one", indicator=True)
joined["matched"] = joined["_merge"].eq("both").astype("boolean")
return joined[["id", "name", "matched"]].reset_index(drop=True)- Reasoning
- Construct typed role and company tables. Require unique nonmissing role IDs, nonmissing company IDs on both sides and unique company-dimension IDs. Merge with an explicit on column, left join, cardinality validation and indicator. Convert the indicator to a nullable matched flag and project a stable output schema. Unmatched names remain missing; they do not become an invented company. Trace two roles at one known company and a third at an unknown company. The output contains exactly three roles, two matched flags and one missing name. Two dimension rows for the same company must be rejected rather than produce six role rows.