Editor

The Complete Trend Report

Advanced · Write the code · from Time Series: Detect Trends and Anomalies in Business Data

From the lesson: Step 6: Generate a Complete Trend Report

A good analysis ends with a clear summary. Decision-makers don't want to see raw DataFrames - they want insights. Let's pull together everything we've built into a single comprehensive trend report.

Read this part of the lesson

Write a function generate_report(df) that takes the sales DataFrame and prints a complete analysis report:

1. "=== SALES TREND REPORT ===" header

2. Overview: date range, total days, total revenue, mean daily sales

3. Trend: first vs last week average, percentage change, direction (UPWARD/DOWNWARD)

4. Weekly Pattern: best and worst day of week

5. Anomalies: count of days with |z-score| > 1.5

6. A final "Recommendation:" line based on the trend direction

Call it with the generated DataFrame.

import pandas as pd
import numpy as np
np.random.seed(42)
dates = pd.date_range('2024-01-01', periods=90, freq='D')
base = 1000 + np.arange(90) * 5
weekend_boost = np.where(dates.dayofweek >= 5, 200, 0)
noise = np.random.normal(0, 80, 90)
sales = np.maximum(base + weekend_boost + noise, 0)
df = pd.DataFrame({'sales': np.round(sales, 2)}, index=dates)
def generate_report(df):
# Build and print the complete trend report
pass
generate_report(df)

More Time Series exercises

All Python exercises · 50 practice problems · Practical Projects quiz