Merge the payments with the scheduled installments, take the oldest unpaid one, subtract it from the reporting date, and cut the result into buckets.
Days past due count from the oldest unpaid installment to the reporting date. In pandas: merge the installments due with the payments received on the loan id and the due date, keep the unpaid ones, take the earliest due date of each loan, subtract it from the reporting date with .dt.days, and sort the result into buckets with pd.cut. On the fictional book of this example, 46 loans outstanding at 2026-09-30, 18.7% of the balance is 30 days or more past due.
A schedule lists every installment of every loan; the payment file lists what was received. A left merge of the installments due by the cutoff with the payments, with indicator=True, marks each installment as paid or not. validate="one_to_one" stops the script if an installment appears twice on either side.
@lru_cache(maxsize=1)
def matched():
s, pays = build_schedule(), load_payments()
due = s[s["due_date"] <= CUTOFF]
m = due.merge(pays, on=["loan_id", "due_date"], how="left",
validate="one_to_one", indicator=True)
m["paid"] = m["_merge"] == "both"
return m.drop(columns="_merge")
def actual_balance():
m, loans = matched(), load_loans()
repaid = m[m["paid"]].groupby("loan_id")["principal"].sum()
start = loans.set_index("loan_id")["principal"].astype(float)
return start.sub(repaid, fill_value=0)
def days_past_due():
m = matched()
oldest = m[~m["paid"]].groupby("loan_id")["due_date"].min()
dpd = (CUTOFF - oldest).dt.days
return dpd.reindex(load_loans()["loan_id"]).fillna(0).astype(int)
@lru_cache(maxsize=1)
def book():
"""One row per loan still outstanding: balance, days past due, bucket."""
b = pd.DataFrame({"balance": actual_balance(), "dpd": days_past_due()})
b = b[b["balance"] > 0.005].copy()
b["bucket"] = pd.cut(b["dpd"], bins=[-1, 0, 29, 59, 89, 10_000], labels=BUCKETS)
return b
The bins are closed on the right: (0, 29] is 1 to 29 days, (29, 59] is 30 to 59. A loan with nothing unpaid gets 0 days and lands in Current. Loans already repaid are left out: they are not current, they are gone.
| Bucket | Loans | Balance | Share |
|---|---|---|---|
| Current | 32 | $29,960,686 | 64.3% |
| 1-29 | 6 | $7,904,899 | 17.0% |
| 30-59 | 3 | $4,589,811 | 9.9% |
| 60-89 | 2 | $1,294,312 | 2.8% |
| 90+ | 3 | $2,819,466 | 6.1% |
| Total | 46 | $46,569,174 | 100.0% |
$2,819,466 is 90 days or more past due, the usual threshold of default; the oldest arrears reach 260 days.
Calder Lane Credit and its loans are fictional; the data were generated by code. Educational material, not investment advice. Prepared with AI assistance.
31 lessons from zero: import, clean and join the files of a finance team, build payment schedules, age arrears, compare vintages, measure yields, draw charts, write the Excel report and reconcile it, on a 60-loan portfolio. Start and solution files, notebooks, exercises and a certificate exam ($199).
As the number of days between the oldest unpaid installment and the reporting date. A loan with no unpaid installment is current, with zero days past due.
With pd.cut on the days past due, for example bins of -1, 0, 29, 59, 89 and a large number, labelled Current, 1-29, 30-59, 60-89 and 90+.
A common threshold is 90 days past due, used by banking supervisors for non-performing exposures; each lender's policy defines its own rule.