A robot inspection report can attach a calibration revision to an observation whose station ID is missing. The error can appear when teams move a report from a database query to a pandas merge. With an ordinary equality join in SQLite, a missing station key does not satisfy the join condition. pandas can match missing keys on both sides. If the calibration table has several provisional entries with missing station IDs, a single inspection can also appear several times in the result.
A missing station ID means that the observation cannot be attributed based on that field. Treating all missing keys as one station creates a relationship that the logs do not establish. The distinction matters when analysts compare yields by cell, look up a calibration revision, or count which observations need supervisor review.
Build a join that preserves observations
The following fixture has four inspection observations. A01 names an existing station, A02 and A03 have missing station IDs, and A04 names an unregistered station. The calibration list includes a revision for S07 plus two provisional revisions without a station assignment. The uppercase codes are invented for the example.
import pandas as pd
observations = pd.DataFrame({
"observation": ["A01", "A02", "A03", "A04"],
"station": ["S07", None, None, "S99"],
})
calibration = pd.DataFrame({
"station": ["S07", None, None],
"revision": ["R12", "PROV1", "PROV2"],
})
raw = observations.merge(calibration, on="station", how="left",
indicator=True)
assert len(raw) == 6
assert (raw["_merge"] == "both").sum() == 5
assert raw.loc[raw["observation"].eq("A02"), "revision"].tolist() == [
"PROV1", "PROV2"
]
usable = calibration.dropna(subset=["station"])
report = observations.merge(usable, on="station", how="left",
validate="many_to_one", indicator=True)
assert len(report) == len(observations)
assert (report["_merge"] == "both").sum() == 1
assert report["observation"].tolist() == observations["observation"].tolist()
In the raw result, each of the two observations with missing station IDs is associated with both provisional records. That produces four rows marked as matches from just two original observations. The remaining match belongs to A01. The filtered lookup still keeps A02 and A03 in the inspection report, but neither receives a revision. A04 also remains unmatched for a different reason. Its station ID is present but absent from the calibration list.
When the report stores a status, give missing station IDs and unrecognized station IDs separate categories. A missing ID may point to an issue in the observation export. An unrecognized ID may point to a missing lookup record or a mismatch in how the two sources encode station IDs. Keep the original source key on the observation so that a later correction can be traced to the source.
Check what cardinality validation actually guarantees
A many-to-one assertion checks whether the join keys on the right are unique for that relationship. It helps detect duplicate calibration entries for a known station. If the unfiltered lookup contains only one provisional record with a missing key, the assertion can pass while pandas still assigns its revision to both observations with missing station IDs. The application must define which keys are eligible to match before checking how many lookups each key may have.
If a known station really has several valid revisions, the station alone is an incomplete key. Resolve the active revision from the recorded revision or effective date before making a many-to-one claim. For a compound key such as site and station, a missing site can be just as consequential as a missing station. Apply the eligibility check to every required column in the key. Treat empty strings and placeholders according to the source format rather than assuming that all spellings are automatically recognized as missing.
Compare reports using the same policy
For the SQLite comparison, put the four observations and three calibration rows into two tables, using SQL NULL for missing values. Select observation and revision with a LEFT JOIN and the condition observations.station = calibration.station. The query returns four rows, with R12 attached to A01 and a NULL revision for the other three observations. The filtered pandas result follows that same policy. An explicit null-safe equality would define a different policy, so record the join predicate alongside the comparison.
Use fixtures with both known and missing keys on both sides. Check the count of source observations and the number retained after the merge. Then check which specific observations have revisions. Matching totals alone can hide a false association, especially when a single provisional lookup row can give the merge the expected row count.
Sources
pandas API reference, merge key behavior, validation, and merge indicator
https://pandas.pydata.org/docs/reference/api/pandas.merge.html
SQLite SELECT documentation, equality predicates and left joins
https://www.sqlite.org/lang_select.html