Open In Colab

Time Series Data It refers to data that is collected, recorded or observed over time in a sequential order.

Characteristics: - Chronological Order : Observations are ordered in time (D, W, M, Y, s, m ,h) - Sequential Dependency : The order of the data matters because previous values can influence or predict the future values.

Time series Analysis: - statistical technique : meaningful insights about pattern and trends - forecasting

Time Series Decomposition - Trend : long term direction - Seasonality : repeting pattern at fixed interval - Noise

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Using airline passenger dataset (monthly totals)
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv'
df = pd.read_csv(url, parse_dates=['Month'], index_col='Month')
df
Passengers
Month
1949-01-01 112
1949-02-01 118
1949-03-01 132
1949-04-01 129
1949-05-01 121
... ...
1960-08-01 606
1960-09-01 508
1960-10-01 461
1960-11-01 390
1960-12-01 432

144 rows × 1 columns

df["Passengers"].plot(figsize = (12,5))

image.png
import yfinance as yf
ticker_symbol = 'RELIANCE.NS'
stock_data = yf.download(ticker_symbol, start='2023-01-01', end='2025-08-01', interval="1d")
stock_data.columns =stock_data.columns.droplevel("Ticker")
stock_data
/tmp/ipython-input-979513816.py:3: FutureWarning: YF.download() has changed argument auto_adjust default to True
  stock_data = yf.download(ticker_symbol, start='2023-01-01', end='2025-08-01', interval="1d")
[*********************100%***********************]  1 of 1 completed
Price Close High Low Open Volume
Date
2023-01-02 1180.586060 1182.006865 1167.890577 1168.715541 5316175
2023-01-03 1171.946655 1179.256896 1167.707271 1175.613232 7658932
2023-01-04 1154.301392 1173.780010 1152.216007 1171.923749 9264891
2023-01-05 1152.239014 1162.482516 1147.632911 1156.570169 13637099
2023-01-06 1162.711548 1167.776018 1154.186834 1158.013796 6349597
... ... ... ... ... ...
2025-07-25 1391.699951 1401.000000 1384.099976 1398.900024 11854722
2025-07-28 1387.599976 1407.800049 1385.000000 1392.300049 7748361
2025-07-29 1417.099976 1420.199951 1383.000000 1383.000000 10750072
2025-07-30 1410.099976 1423.300049 1401.300049 1418.099976 7209849
2025-07-31 1390.199951 1402.599976 1382.199951 1388.099976 17065827

637 rows × 5 columns

type(stock_data.index)
pandas.core.indexes.datetimes.DatetimeIndex
stock_data["Close"].plot(figsize = (12,5))

from statsmodels.tsa.seasonal import seasonal_decompose

sd = seasonal_decompose(df["Passengers"], model="multiplicative", period = 10)
trend = sd.trend
seasonal = sd.seasonal
residuals = sd.resid
plt.figure(figsize=(14,10))
plt.subplot(411)
plt.plot(df["Passengers"])
plt.subplot(412)
plt.plot(trend)
plt.subplot(413)
plt.plot(seasonal)
plt.subplot(414)
plt.plot(residuals)
plt.show()

sd = seasonal_decompose(stock_data["Close"], model="additive", period = 10)
trend = sd.trend
seasonal = sd.seasonal
residuals = sd.resid
import matplotlib.pyplot as plt
plt.figure(figsize=(14,10))
plt.subplot(411)
plt.plot(stock_data["Close"])
plt.subplot(412)
plt.plot(trend)
plt.subplot(413)
plt.plot(seasonal)
plt.subplot(414)
plt.plot(residuals)
plt.show()

import matplotlib.pyplot as plt

estimated = trend + seasonal
plt.plot(stock_data["Close"], color="blue")
plt.plot(estimated,  color="red")
plt.show()

estimated.fillna(0)
0
Date
2023-01-02 0.0
2023-01-03 0.0
2023-01-04 0.0
2023-01-05 0.0
2023-01-06 0.0
... ...
2025-07-25 0.0
2025-07-28 0.0
2025-07-29 0.0
2025-07-30 0.0
2025-07-31 0.0

637 rows × 1 columns


from sklearn.metrics import mean_squared_error
mean_squared_error(stock_data["Close"], estimated.fillna(0))
26253.988735045932