Complete worked example
python
import pandas as pd
def inspect_example(rows):
if any(type(r.get("id")) is not str or not r["id"].strip() or type(r.get("seq")) is not int or r["seq"] < 0 for r in rows):
raise ValueError("invalid observation identity")
frame = pd.DataFrame(rows, columns=["id", "updated", "seq", "score"])
if frame.duplicated(["id", "seq"]).any():
raise ValueError("duplicate observation sequence")
frame = frame.astype({"id": "string", "score": "Float64"})
frame["updated"] = pd.to_datetime(frame["updated"], format="ISO8601", utc=True, errors="raise")
ordered = frame.sort_values(["id", "updated", "seq"], kind="stable")
latest = ordered.drop_duplicates("id", keep="last")
return latest[["id", "score"]].reset_index(drop=True)- Reasoning
- Validate nonblank string IDs, nonnegative integer sequences and uniqueness of each ID-sequence pair. Parse updated timestamps as UTC using an explicit ISO8601 format. Sort by ID, timestamp and sequence, then drop duplicate IDs while retaining the last row. Finally project ID and nullable score and reset the presentation index. A stable sort is requested so the rule remains understandable even before every tie is eliminated. Trace two observations of a and one of b, supplied out of time order. The newest a must win regardless of which row arrived last in the list. An equal-time a observation uses the larger sequence value.