Many first data science jobs involve one question: what happens next month? Sales, website visits, call-centre volume and stock levels are all measured over time. This guide explains time series forecasting in plain terms and shows how to build a first forecast in Python, step by step.
What is a time series?
A time series is a set of values recorded at regular intervals: daily orders, weekly sign-ups, monthly revenue. Two things make it different from the tables you use in most machine learning work:
- Order matters. Each row depends on the rows before it, so you cannot shuffle the data.
- The future is unknown. You can only test a model on data that comes after the data it learned from.
The four parts of a time series
- Trend: the long-term direction, such as steady growth in online orders.
- Seasonality: a pattern that repeats on a fixed calendar, such as weekend peaks or a festival-season rise.
- Cycles: longer ups and downs without a fixed length, often linked to the economy.
- Noise: random variation that no model can predict.
Plot the data first. A simple line chart usually shows the trend and seasonality before you write any model code.
Step 1: prepare the data in pandas
import pandas as pd
df = pd.read_csv("daily_sales.csv", parse_dates=["date"])
df = df.set_index("date").sort_index()
df = df.asfreq("D") # one row per day
df["sales"] = df["sales"].interpolate() # fill small gaps
Check for missing dates, duplicate dates and sudden jumps caused by data entry errors. These problems damage a forecast more than the choice of model does.
Step 2: build a baseline forecast
Always start with a simple forecast that any model must beat:
- Naive: tomorrow equals today.
- Seasonal naive: next Monday equals last Monday.
- Moving average: the average of the last 7 or 28 days.
train = df[:"2025-12-31"]
test = df["2026-01-01":]
baseline = train["sales"].iloc[-7:].mean()
If a complex model cannot beat the baseline, it is not ready to use.
Step 3: split by time, not at random
In normal machine learning you split rows at random. In forecasting you must train on the past and test on the future. For a fair check, use walk-forward validation: train up to month 6 and test month 7, then train up to month 7 and test month 8, and so on. scikit-learn’s TimeSeriesSplit does this for you.
Step 4: try standard models
- Exponential smoothing (Holt-Winters): gives more weight to recent values and handles trend and seasonality. A good first model.
- ARIMA and SARIMA: model each value from past values and past errors. SARIMA adds seasonality. Available in the
statsmodelslibrary. - Regression with lag features: create columns such as “sales 7 days ago”, “day of week” and “is holiday”, then use a model like gradient boosting.
from statsmodels.tsa.holtwinters import ExponentialSmoothing
model = ExponentialSmoothing(train["sales"], trend="add",
seasonal="add", seasonal_periods=7).fit()
forecast = model.forecast(len(test))
Step 5: measure the error
- MAE (mean absolute error): the average size of the miss, in the same units as your data. Easy to explain to a manager.
- RMSE: punishes large misses more than MAE.
- MAPE: error as a percentage. Avoid it when actual values can be zero or very small.
Compare every model against the baseline on the same test period.
Common beginner mistakes
- Shuffling rows before splitting, which leaks future information into training.
- Building features with values that would not be known at forecast time.
- Reporting one number without a range. Give a likely range, not just a single value.
- Ignoring holidays and special events that explain spikes.
A portfolio project idea
Take a public dataset of daily sales or website visits. Build a seasonal naive baseline, a Holt-Winters model and a gradient boosting model with lag features. Compare them with walk-forward validation and MAE. Write one page explaining which model you would use and why. That page shows an interviewer how you think, not just which library you know.
Where this fits in your learning
Forecasting builds on pandas, train-test splitting and model evaluation. If those are new to you, start with our machine learning for beginners guide, especially the sections on train-test split and model evaluation. For project ideas across data roles, see the IT career guide for freshers.
To learn data science with guided projects in a classroom in BTM Layout, see the Data Science course in Bangalore or book a free demo class. For fees and batch dates, please contact a counsellor.
