Articles

How do you compute days past due and aging buckets in pandas?

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.

Step 1: find the unpaid installments

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)

Step 2: days past due and buckets

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.

The result

Calder Lane Credit (fictional), 2026-09-30: balance by days past due.
BucketLoansBalanceShare
Current32$29,960,68664.3%
1-296$7,904,89917.0%
30-593$4,589,8119.9%
60-892$1,294,3122.8%
90+3$2,819,4666.1%
Total46$46,569,174100.0%

$2,819,466 is 90 days or more past due, the usual threshold of default; the oldest arrears reach 260 days.

Three ways to get it wrong

Sources

  1. Julian R. Sterling, Python & pandas for Finance (course, 2026): running case Calder Lane Credit, fictional
  2. pandas documentation: merge (validate, indicator), groupby, cut, read_excel (consulted November 2026)
  3. Basel Committee on Banking Supervision, Guidelines on prudent treatment of problem assets (2017)

Calder Lane Credit and its loans are fictional; the data were generated by code. Educational material, not investment advice. Prepared with AI assistance.

Learn Python and pandas on a real loan book

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).

Read module 1 free →

FAQ

How are days past due calculated?

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.

How do you create aging buckets in pandas?

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+.

When is a loan considered in default?

A common threshold is 90 days past due, used by banking supervisors for non-performing exposures; each lender's policy defines its own rule.

Related