This page is your companion to the Lab 7 Colab notebook. It shows the code in color and lists every function's specification in one place. Do the work in the notebook; read the color here.
Every function in the lab is graded for completeness. A complete function has a clear name, well-named inputs and variables, a docstring, and returns the right value:
def amount_with_tax(amount, tax_rate):
"""Return the amount grossed up by the tax rate (0.0725 means 7.25%)."""
grossed_up = amount * (1 + tax_rate)
return grossed_up
amount, tax_rate, grossed_up), not x / y / tmp,def saying what it returns,For each, the stub is given to you. Replace the empty docstring with one
sentence and return None with the real body.
is_number(value)True if the value is an int or float, else False. Example: is_number(5) → True, is_number("5") → False.def is_number(value):
""" """
return None
aging_bucket(days_past_due)Not due): Not due, 0-30, 31-60, 61-90, 91-120, 121-150, 151-180, 181+. Example: aging_bucket(45) → "31-60". Boundaries: 30 is 0-30, 31 starts 31-60.def aging_bucket(days_past_due):
""" """
return None
running_total(amounts)for loop and += (not sum()). Example: running_total([10, 20, 30]) → 60. An empty list returns 0, not None.def running_total(amounts):
""" """
return None
line_total(qty, price) and amount_with_tax(amount, tax_rate)line_total returns qty * price — line_total(2, 600.0) → 1200.0. amount_with_tax returns amount * (1 + tax_rate) — amount_with_tax(100, 0.0725) → 107.25.def line_total(qty, price):
""" """
return None
def amount_with_tax(amount, tax_rate):
""" """
return None
grand_total(pairs)line_total(qty, price) over a list of (qty, price) tuples. Example: grand_total([(2, 10.0), (3, 5.0)]) → 35.0.def grand_total(pairs):
""" """
return None
total_amount(df)amount column of an invoices DataFrame (df["amount"].sum()). Example: total_amount(sample_ar) → 12200.0.def total_amount(df):
""" """
return None
safe_line_total(qty, price)qty * price when both are numbers; None if either is not. Example: safe_line_total(7, 12.5) → 87.5, safe_line_total("oops", 5) → None.def safe_line_total(qty, price):
""" """
return None
large_invoices(df, threshold)amount is strictly greater than threshold — a boolean mask.def large_invoices(df, threshold):
""" """
return None
total_by_segment(df)amount per segment — the PivotTable, in one line.def total_by_segment(df):
""" """
return None
After you rebuild the aging in pandas, run the equivalent SQL on the
Week 3 SQL workbench
and confirm the bucket totals match your aging_pandas, bucket for bucket.
SELECT
CASE
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 0 THEN 'Not due'
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 30 THEN '0-30'
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 60 THEN '31-60'
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 90 THEN '61-90'
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 120 THEN '91-120'
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 150 THEN '121-150'
WHEN CAST(julianday('2024-09-30') - julianday(due_date) AS INT) <= 180 THEN '151-180'
ELSE '181+'
END AS aging_bucket,
ROUND(SUM(amount), 2) AS total_receivable
FROM invoices
GROUP BY aging_bucket
ORDER BY total_receivable DESC;
| Excel move | Python you write this lab |
|---|---|
=B2*C2 (a named formula) | def line_total(qty, price): return qty * price |
| running-total column, drag down | running += a in a for loop |
=SUM(D2:D260) | df["amount"].sum() |
=IF(D2>10000, ...) then filter | df[df["amount"] > 10000] |
| XLOOKUP customer onto invoice | df.merge(customers, on="customer_id") |
| PivotTable summing by segment | df.groupby("segment")["amount"].sum() |
| Week 5 aging bucket | aging_bucket(days) (the same labels) |
Week 7 of ACCTG 5150, Accounting Analytics. Code colorized with highlight.js.