HomeCourseModule 15 › User story: PTO accrual and balances

User story: PTO accrual and balances

Module 15 · Scenario: HR & Operations9 min readIntermediate

What you'll learn

  • Compute accrual based on tenure
  • Subtract taken leave
  • Flag anyone over the cap

The rules (example)

Compute

from datetime import date
import pandas as pd

emp = pd.read_excel("employees.xlsx")
leave = pd.read_excel("leave_taken.xlsx")   # employee_id, days_taken_ytd

today = pd.Timestamp(date.today())
emp["tenure_yrs"] = ((today - pd.to_datetime(emp["hire_date"])).dt.days / 365.25)

def annual_rate(years):
    if years >= 10: return 25
    if years >= 5:  return 20
    return 15

emp["annual_rate"] = emp["tenure_yrs"].apply(annual_rate)
ytd_fraction = (today.dayofyear / 365.25)
emp["accrued_ytd"] = (emp["annual_rate"] * ytd_fraction).round(1)

emp = emp.merge(leave, on="employee_id", how="left")
emp["days_taken_ytd"] = emp["days_taken_ytd"].fillna(0)
emp["balance"] = (emp["accrued_ytd"] - emp["days_taken_ytd"]).round(1)

emp["over_cap"] = emp["balance"] > 30
print(f"{emp['over_cap'].sum()} employees are over the 30-day cap.")

emp[["employee_id","name","tenure_yrs","annual_rate","accrued_ytd","days_taken_ytd","balance","over_cap"]].to_excel("pto_balances.xlsx", index=False)

Key takeaways

  • Tiered accrual = a function of tenure, applied with .apply().
  • YTD accrual is a simple proration.
  • Always flag exceptions (over-cap, negative balance, > 1yr no leave).

Use-it-or-lose-it warning

Find employees who would lose more than 5 days at year-end if they don't take any more leave. Email each one a polite nudge using Module 12 Lesson 5.

📹 Video walkthrough
A video walkthrough of this lesson will be embedded here. Until then, the written walkthrough above mirrors what the video will cover step-for-step.