Introduction to Time Series
A time series is a sequence of observations collected over time, where the order matters. In time series, yesterday can influence today, and today can influence tomorrow.
Examples you already know:
- Daily stock prices
- Monthly sales
- Hourly temperature readings
- Website traffic per minute
Why Time Series Is Different from Regular Data
Time series data is special because rows are time-dependent. That’s why we do not treat it like a normal dataset.
| Regular Data | Time Series Data |
|---|---|
| Rows are independent | Rows depend on time order |
| Order doesn’t matter | Order is critical |
| Shuffling is okay | Shuffling breaks meaning |
| Standard ML works directly | Special methods are needed |
Step 1: Creating a Simple Time Series (Python)
First, we will create a basic monthly time series using Pandas. This is a small example of “monthly sales” stored with dates.
What we are doing here:
- Create a monthly date range
- Store numeric values (sales)
- Combine them into a Pandas
Serieswith a time index
import pandas as pd
import matplotlib.pyplot as plt
# Create date range (monthly)
dates = pd.date_range(start="2023-01-01", periods=12, freq="M")
# Sample values (monthly sales)
values = [120, 135, 150, 145, 160, 170, 180, 175, 190, 200, 210, 220]
# Create time series
ts = pd.Series(values, index=dates)
print(ts)
Output:
2023-01-31 120
2023-02-28 135
2023-03-31 150
2023-04-30 145
2023-05-31 160
2023-06-30 170
2023-07-31 180
2023-08-31 175
2023-09-30 190
2023-10-31 200
2023-11-30 210
2023-12-31 220
Freq: M, dtype: int64
What Happened in the Output? (Very Important)
Let’s decode the output clearly:
- Left side: the date (this is the time index)
- Right side: the value (sales for that month)
- Freq: M means Pandas detected this as monthly data
At this stage, we only created the data. We have not analyzed anything yet. Next, we will visualize it.
Step 2: Visualizing the Time Series (Mandatory)
Time series analysis always starts with visualization. Before any model, we must see the data to identify trends, spikes, or patterns.
plt.figure(figsize=(10, 4))
plt.plot(ts, marker='o')
plt.title("Monthly Sales Over Time")
plt.xlabel("Date")
plt.ylabel("Sales")
plt.grid(True)
plt.show()
What Plot Should You See?
After running the plotting code, you should see a line chart where the sales values move upward over time. It will look like a rising trend line with small ups and downs.
This is a visual expectation of the plot shape. Your actual Matplotlib chart will be rendered by Python.
How to Interpret This Plot
Even from one simple plot, we can learn a lot:
- Overall upward trend: sales are increasing over time
- Small drops: normal fluctuations (not every month grows)
- No repeating cycle visible: seasonality is not obvious yet
This is exactly why visualization comes first. Before forecasting, we must understand the data behavior.
Where to Run This Code (Practice Like Us)
To practice this lesson properly, run the code in any one of these:
- Jupyter Notebook (best for learning)
- Google Colab (no setup needed)
- VS Code (Python environment)
Install required libraries:
pip install pandas matplotlib
Practice Questions (Homework)
Q1. Why can’t we shuffle time series data like normal datasets?
Q2. What does Freq: M in the output mean?
Q3. What are two insights you can get from a time series plot before modeling?
Quick Quiz
Q1. In time series, what matters the most?
Q2. What is the first step of time series analysis?
Quick Recap
- A time series is data collected over time where order matters
- Time series is different from regular data because it is time-dependent
- We created a monthly time series using Pandas
- We plotted the series to detect trend and behavior
- Visualization is mandatory before forecasting