๐Ÿ“ก
Grace's Story ยท Network KPIs
You work as a business analyst at a national telecom operator's network team

Every half hour, your system logs KPIs: active subscribers, call minutes, SMS count, data usage, and call drop rate. You're staring at 480 rows of chaotic half-hourly data and need to understand weekly trends, smooth out noise, spot incidents, and compare day-over-day performance. That's time-series work.

480
Half-hourly rows
10
Days observed
3
Regions (Nairobi, Mombasa, Kisumu)
What you'll master in this lesson
  • Index time data with pd.to_datetime() and DatetimeIndex
  • Resample to different frequencies (daily, weekly, hourly) with .resample()
  • Smooth data with rolling windows and understand lag
  • Compare periods with .shift(), .diff(), .pct_change()
  • Spot trends, seasonality, and anomalies in network traffic
  • Know when resampling is better than groupby
๐Ÿ“Š
network_metrics.csv
480 rows ยท 7 columns ยท half-hourly telecom KPIs

1. Why time-series is different

The shape of your data tells a story before you write a single line of code.

Half-hourly call_minutes over 10 days (Nairobi region)
Half-hourly call minutes showing daily oscillations and weekend dips
What this shows: Every day has a wobble (18:00โ€“21:00 peak), and weekends dip. On Feb 5, you see a flat spot (incident). Row 1 and row 361 aren't directly comparable โ€” you need to align them by date first. That's what DatetimeIndex does.

Traditional groupby won't work here because time isn't just a category โ€” it's a sequence. When you resample, you're respecting the order and frequency of dates. Missing dates get filled (or flagged). That's the superpower.

2. DatetimeIndex is the gateway

Before resampling, your timestamp column must become the index, and it must be a DatetimeIndex.

Before: raw timestamps

timestamp region call_minutes
2026-02-01 00:00:00Nairobi8,220
2026-02-01 00:30:00Mombasa5,145
2026-02-01 01:00:00Kisumu2,650

The timestamp is just a column โ€” it's data, not a navigator.

After: DatetimeIndex

timestamp (index) region call_minutes
2026-02-01 00:00:00Nairobi8,220
2026-02-01 00:30:00Mombasa5,145
2026-02-01 01:00:00Kisumu2,650
What this shows: Now the index is time-aware. Pandas knows Feb 01 comes before Feb 02. You can slice by date ranges, resample, and roll forward in time. df.loc['2026-02-05'] returns all rows from Feb 5 โ€” no filtering needed.

3. .resample() โ€” the time groupby

.resample() groups by time buckets. 'D' = daily, 'W' = weekly, 'H' = hourly. Each bucket can be aggregated with .sum(), .mean(), .max(), etc.

Three resampling frequencies

Frequency Example dates Use case
'H' (hourly) 2026-02-01 00:00, 01:00, โ€ฆ Peak hour analysis, congestion patterns
'D' (daily) 2026-02-01, 2026-02-02, โ€ฆ Trend, daily seasonality, YoY compare
'W' (weekly) 2026-02-01, 2026-02-08 Long-term trends, campaign impact
Daily call_minutes total across all regions
Daily total call minutes showing incident on Feb 5 and weekend dip
What this shows: Each bar is a daily sum of call_minutes from all three regions. Feb 5 (incident day) is shorter. Weekends (Feb 6โ€“7) also dip. Resampling compresses 48 half-hourly values into one daily value, making patterns visible.

4. Rolling windows

A rolling window smooths noise by computing a moving average. Over half-hourly data, a 48-period window = 24 hours (rolling average of the past day).

The rolling mean lags because it looks back. Row 100 shows the mean of rows 52โ€“100. This is useful for trend detection but misleading for real-time alerting.

Raw vs. 24-hour rolling mean of call_minutes
Raw call minutes vs rolling average showing smoothed trend
What this shows: The purple line is raw data (jagged). The amber dashed line is the rolling mean (smooth). Rolling reveals the true trend under the daily wobble. Use this for spotting long-term shifts โ€” but the rolling mean starts at row 48, so it can't warn you about the first 24 hours.

5. Resampling aggregation options

When you resample, you pick how to combine rows in each bucket. Different aggregations reveal different patterns.

Date sum() mean() max() Insight
2026-02-01195,2406,8419,220Normal day, peak at 9,220
2026-02-02198,5606,9559,480Slightly busier
2026-02-05176,8006,1887,950Incident! Both sum and max down
2026-02-07165,4805,7938,120Weekend dip
What this shows: sum() tells you total volume (capacity planning). mean() tells you average intensity. max() flags peaks (QoS issues). Feb 5 shows all three down โ€” a real problem, not just a timing shift. Use .agg(['sum', 'mean', 'max']) to compare all three at once.

6. Shift, Diff, Pct_Change โ€” comparing periods

To find day-over-day change, you shift the series down one row, then subtract. This reveals if a day is up or down from yesterday.

.diff() is shorthand for .shift(1).subtract(...). .pct_change() computes percent change. Feb 5 will show a dip (negative change) because of the incident.

Day-over-day % change in active_subscribers
Day-over-day percentage change in subscribers showing incident impact
What this shows: Green bars are growth day-over-day. The red bar on Feb 5 is the incident โ€” a -2.8% drop in active subscribers from Feb 4. By Feb 8, growth is back to normal (+1.8%). This chart lets you spot when something went wrong and when recovery happened โ€” a critical operational metric.
๐ŸŽฏ
Check your understanding
Q1: What's the difference between .resample('D').sum() and .groupby(df.index.date).sum()?
They're identical โ€” just different ways to write the same thing.
Resample respects time frequency and fills missing dates; groupby only groups existing dates.
Groupby is faster and resample is slower.
Resample only works with numeric columns.
Q2: What does .rolling(7).mean() compute on daily data?
The mean of the next 7 days.
The mean of the past 7 days (including today).
A single 7-day average for the entire series.
The mean excluding weekends.
Q3: Your code is df['value'].shift(1).diff(). What's wrong?
Shift and diff can't be chained โ€” use only one.
Nothing โ€” but .diff() alone is simpler; it already does the shift internally.
Shift returns NaN; diff will fail on NaN.
The order is backwards โ€” diff should come before shift.
0
/3
Great start!

You're ready to move to correlation and feature relationships.