# Import the table toolkit for the analysis.
import pandas as pd
# Load the service log into a table.
df = pd.read_csv("service_times.csv")
# Verify that a ticket is not counted twice.
assert df["ticket_id"].is_unique, "Duplicate ticket IDs"
# Select the numeric service duration.
times = df.service_minutes
# Check that every duration is present.
assert times.notna().all(), "Missing durations"
# Check the valid range of elapsed minutes.
assert times.gt(0).all(), "Nonpositive duration"
# Record the number of usable durations.
n = times.count()
# Calculate the arithmetic center.
mean = times.mean()
# Calculate the rank-based center.
median = times.median()
# Find every most frequent duration.
modes = times.mode().tolist()
# Calculate sample variance.
variance = times.var(ddof=1)
# Calculate sample standard deviation.
std = times.std(ddof=1)
# Measure the direction of asymmetry.
skew = times.skew()
# Measure standardized fourth-moment tail behavior.
kurt = times.kurt()
# Check agreement between spread measures.
assert abs(std ** 2 - variance) < 1e-9, "Variance mismatch"
# Flag tickets exceeding forty minutes.
late = times.gt(40)
# Convert the exception flags into a percentage.
late_rate = late.mean() * 100
# Prepare center-level summaries.
groups = df.groupby("center")["service_minutes"]
# Calculate comparable center-level summaries.
summary = groups.agg(["count", "mean", "median", "std", "var", "skew"])
# Add Fisher excess kurtosis by center.
summary["kurtosis"] = groups.apply(lambda s: s.kurt())
# Add all tied modes for each center.
summary["mode"] = groups.apply(lambda s: s.mode().tolist())
# Calculate the exception rate separately for each center.
summary["late_pct"] = groups.apply(lambda s: s.gt(40).mean() * 100)
# Choose the center with the highest exception rate.
priority = summary["late_pct"].idxmax()
# Display the count and measures of center.
print("Tickets:", n, "| Mean:", round(mean, 2), "| Median:", round(median, 2), "| Mode:", modes)
# Display spread, shape, exception rates and the review priority.
print("Variance:", round(variance, 2), "| SD:", round(std, 2), "| Skew:", round(skew, 2), "| Excess kurtosis:", round(kurt, 2), "| Late %:", round(late_rate, 2), "\n", summary.round(2).to_string(), "\nReview priority:", priority)
