# Import pandas to analyze the parcel dispatch log.
import pandas as pd
# Read the CSV and parse both timestamps so they can be compared.
log = pd.read_csv("dispatch_log.csv", parse_dates=["cutoff_at", "departed_at"])
# Check that each parcel ID appears only once.
assert log["parcel_id"].is_unique, "Repeated parcel ID"
# Require a value in every dispatch field before calculating rates.
assert log.notna().all().all(), "Missing dispatch field"
# Check that all depot names belong to the three expected locations.
assert log["depot"].isin(["Dhanmondi", "Mirpur", "Uttara"]).all(), "Unknown depot"
# Check that every shift is labeled AM or PM.
assert log["shift"].isin(["AM", "PM"]).all(), "Unknown shift"
# Flag a parcel as late when it departs after its cutoff time.
log["late"] = log["departed_at"].gt(log["cutoff_at"])
# Count parcels for each depot and shift to get the total volume.
volume = log.pivot_table(index="depot", columns="shift", values="parcel_id", aggfunc="count")
# Sum the late flags to count late parcels for each depot and shift.
misses = log.pivot_table(index="depot", columns="shift", values="late", aggfunc="sum")
# Check that the volume table accounts for every parcel in the log.
assert volume.sum().sum() == len(log), "Volume does not reconcile"
# Check that the late-count table accounts for every flagged parcel.
assert misses.sum().sum() == log["late"].sum(), "Late count does not reconcile"
# Divide late counts by matching parcel totals and convert to percentages.
rate = misses.div(volume).mul(100)
# Calculate the percentage of all parcels that departed late.
network_rate = 100 * log["late"].mean()
# Stack the late counts into a series indexed by depot and shift.
counts = misses.stack()
# Stack the late percentages into a series indexed by depot and shift.
rates = rate.stack()
# Find the depot and shift with the largest late count, taking the first tie.
most_late = counts.idxmax()
# Find the depot and shift with the highest late percentage, taking the first tie.
highest_rate = rates.idxmax()
# Calculate the selected shift’s share of all late parcels across the network.
share = 100 * counts.loc[most_late] / counts.sum()
# Display the parcel volume table with depots as rows and shifts as columns.
print("PARCELS", volume.to_string(), sep="\n")
# Display the late parcel counts for each depot and shift.
print("LATE PARCELS", misses.to_string(), sep="\n")
# Display the late percentages rounded to two decimal places.
print("LATE RATE (%)", rate.round(2).to_string(), sep="\n")
# Print the network-wide late percentage to two decimal places.
print(f"Network late rate: {network_rate:.2f}%")
# Report the depot and shift with the largest number of late parcels and its count.
print(f"Most late: {most_late[0]} {most_late[1]} | {counts.loc[most_late]} parcels")
# Report the depot and shift with the highest late percentage and its rate.
print(f"Highest rate: {highest_rate[0]} {highest_rate[1]} | {rates.loc[highest_rate]:.2f}%")
# Print the share of all late parcels covered by reviewing the selected depot and shift.
print(f"Share of all late parcels in first review: {share:.2f}%")
