KatInterview

Radiology Claims Spend Summary with Fiscal-Month Mapping in pandas

SQL & DataModel answer
MediumModel answerSQL & Datapandasdata-cleaninggroupby

Last updated

Clean a small claims CSV in pandas: sum spend per procedure group, compute each group's share of the total, and derive an October-based fiscal month without row loops.

You are given a small export of radiology claims. Each row has a claim id, a procedure group, a service timestamp, and the amount paid. Timestamps arrive as plain strings (object dtype), some amounts are negative because they are adjustments, and some amounts are blank.

claim_id,procedure_group,service_dt,paid_amt
1001,CT,2020-07-01 13:05:00,120.00
1002,MRI,2020-10-15 09:30:00,250.00
1003,CT,2020-10-20 11:00:00,-20.00
1004,XRay,2020-12-05 08:00:00,80.00
1005,MRI,2020-01-02 14:10:00,

Using Python and pandas, do the following.

  1. Load and summarize. Read the data defensively, with explicit dtypes and deliberate date handling. Treat a blank paid_amt as 0 for aggregation, keep negative amounts as they are, and make sure type conversion does not introduce stray NaNs. Produce a table with procedure_group and paid_amt_sum (rounded to 2 decimals).
  2. Share of total. Add pct_of_total, each group's share of the overall paid amount on a 0 to 100 scale with 2 decimals. The total must come from the cleaned values and must not be double-counted.
  3. Fiscal month. Convert service_dt to a real datetime and add an integer column fiscal_month. The fiscal year starts on October 1, so October is 1, November is 2, and so on through September, which is 12. Unparseable or missing dates should become NaT, and their fiscal_month should be 0 (unknown). Print the final dtypes and show that the computation is vectorized, with no per-row Python loops.

A reviewer's note says that 2020-10-15 "should come out as fiscal_month = 2". Check that claim against the rule above and say what you do about it.

State your assumptions and explain your choices, including the pitfalls you avoided.

Hints

Hint 1

Read `service_dt` as a string and convert it yourself with `pd.to_datetime(..., errors='coerce')`. That way a single bad value cannot silently leave the whole column as `object`.

Hint 2

Fill missing `paid_amt` once, right after loading, and derive everything from that cleaned column. Aggregate first, take the total as the sum of the unrounded group sums, then round only for display.

Hint 3

A month-to-fiscal-month mapping with October as the start is a modular expression on `dt.month`: shift by the start month, wrap with `% 12`, and add 1. Then apply `fillna(0).astype('int64')`. Work out what October maps to under this rule and compare it with the reviewer's claim.