Ravindra BagaleCourses & study guides मराठी Track your progress

65. How to Forecast Sales with Time Series in Python (pandas and sklearn)

65.6 Train/Test Split by Time (Never Shuffle Dates)

Why. Chapter 31 taught train_test_split. For forecasting, random shuffle leaks the future. October can train a model that later “predicts” September.

What. Cut by date: all train months < all test months. No shuffle.

How.

Time cut checklist

  1. Sort by date.
  2. Choose a split date (here: 2026-05-01).
  3. Train = before split; test = on/after split.
  4. Fit only on train.
  5. Score only on test.
  6. Never use shuffle=True for month forecasts.

Worked example A — Correct time split.

import pandas as pd

df = pd.read_csv("mauli_monthly_sales.csv", parse_dates=["month"]).sort_values("month")
split = pd.Timestamp("2026-05-01")
train = df[df["month"] < split]
test = df[df["month"] >= split]
print("train", train["month"].min().date(), "→", train["month"].max().date(), "n=", len(train))
print("test ", test["month"].min().date(), "→", test["month"].max().date(), "n=", len(test))

Output you should see.

train 2023-11-01 → 2026-04-01 n= 30
test  2026-05-01 → 2026-10-01 n= 6

Worked example B — Shuffle vs time MAE (same lag model).

On this lab, time-based test MAE ≈ ₹7,258. A shuffled holdout gave MAE ≈ ₹13,241. Numbers move when the split is dishonest — managers need the time score.

# idea only: compare MAE from time split vs train_test_split(..., shuffle=True)
# Always report the time-based MAE for forecasts.

Worked example C — Interview line.

“I never shuffle a forecasting split. I train on the past and test on the future so the score matches production.”

Ravindra Bagale's Tip

If the future leaks into training, the score is fake. Cut by date. Shuffle is a red flag for forecasts.