Complete worked example
python
import pandas as pd
def inspect_example(ids, base, bonus_ids, bonuses):
if len(ids) != len(base) or len(bonus_ids) != len(bonuses):
raise ValueError("mismatched lengths")
if len(set(ids)) != len(ids) or len(set(bonus_ids)) != len(bonus_ids):
raise ValueError("duplicate identity")
frame = pd.DataFrame({"id": pd.array(ids, dtype="string"),
"base": pd.array(base, dtype="Int64")}).set_index("id")
bonus = pd.Series(bonuses, index=bonus_ids, dtype="Int64")
frame["total"] = frame["base"] + bonus
return frame.reset_index()[["id", "total"]]- Reasoning
- Construct the left table with nullable integer values and set id as its index. Construct the bonus Series with its own IDs as labels. Adding the two Series uses label alignment, and assigning that result to the left frame retains the left population. Finally reset the index so identity becomes an ordinary output column again. Explicit length and uniqueness checks reject ambiguous inputs. Trace left IDs b, a with base values ten, twenty and right IDs a, b with bonuses one, two. The output is b with twelve and a with twenty-one. A result of eleven and twenty-two reveals positional rather than labelled arithmetic.