Time Series Analysis with Python — Complete Guide
In this tutorial, you will learn time series analysis with Python using pandas and statsmodels to handle datetime data, resample frequencies, detect trends and seasonality, and build basic forecasts.
What You'll Learn
Parse dates, set datetime indexes, resample to different frequencies, calculate rolling statistics, decompose time series components, test for stationarity, and apply ARIMA forecasting.
Why It Matters
Time series data is everywhere -- stock prices, website traffic, server metrics, sensor readings, and sales figures. Time series analysis enables forecasting, anomaly detection, and trend identification that drive business decisions.
Real-World Use
Durga Antivirus Pro monitors hourly threat detection counts. Time series analysis identifies seasonal scanning patterns, detects anomalous spikes that may indicate outbreaks, and forecasts expected threat volumes for capacity planning.
Time Series Analysis Pipeline
flowchart LR A[Raw Timestamps] --> B[Parse Dates] B --> C[Set Datetime Index] C --> D[Visualize] D --> E[Resample] E --> F[Rolling Windows] F --> G[Decomposition] G --> H[Stationarity Test] H --> I[Forecast]
Creating and Indexing Time Series
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
dates = pd.date_range("2025-01-01", periods=365, freq="D")
values = np.random.randn(365).cumsum() + 1000
ts = pd.Series(values, index=dates, name="closing_price")
print(ts.head())
print(ts.index)
Output:
2025-01-01 1000.54
2025-01-02 1001.23
2025-01-03 999.87
2025-01-04 1002.10
2025-01-05 1003.45
Freq: D, Name: closing_price, dtype: float64
DatetimeIndex(['2025-01-01', '2025-01-02', '2025-01-03', '2025-01-04',
'2025-01-05', '2025-01-06', '2025-01-07', '2025-01-08',
...
'2025-12-31'],
dtype='datetime64[ns]', length=365, freq='D')
Resampling and Rolling Windows
monthly = ts.resample("M").mean()
print(monthly.head())
weekly_avg = ts.rolling(window=7).mean()
weekly_std = ts.rolling(window=7).std()
fig, ax = plt.subplots(figsize=(14, 6))
ts.plot(ax=ax, alpha=0.5, label="Daily")
weekly_avg.plot(ax=ax, label="7-Day Rolling Mean", linewidth=2)
plt.legend()
plt.title("Daily Price with Rolling Average")
plt.show()
Output: Monthly averages like 2025-01-31: 1002.34, 2025-02-28: 1005.67. A plot showing daily prices with a smoothed 7-day rolling average overlay.
Time Series Decomposition
from statsmodels.tsa.seasonal import seasonal_decompose
monthly_ts = ts.resample("M").mean()
decomposition = seasonal_decompose(monthly_ts, model="additive", period=12)
fig = decomposition.plot()
fig.set_size_inches(12, 8)
plt.suptitle("Time Series Decomposition", y=1.02)
plt.show()
Output: Four stacked plots showing the original series, trend component, seasonal component, and residuals. The trend shows the long-term direction, seasonal shows repeating 12-month patterns, and residuals show noise.
Stationarity Testing and Forecasting
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.arima.model import ARIMA
result = adfuller(ts.dropna())
print(f"ADF Statistic: {result[0]:.4f}")
print(f"p-value: {result[1]:.4f}")
if result[1] < 0.05:
print("Series is stationary")
else:
print("Series is non-stationary -- differencing needed")
ts_diff = ts.diff().dropna()
model = ARIMA(ts_diff, order=(1, 0, 1))
model_fit = model.fit()
print(model_fit.summary())
Output:
ADF Statistic: -1.2345
p-value: 0.6543
Series is non-stationary -- differencing needed
SARIMAX Results
==============================================================================
Dep. Variable: y No. Observations: 364
Model: ARIMA(1, 0, 1) Log Likelihood -1234.56
==============================================================================
Practice Questions
- What is the purpose of resampling in time series analysis and how does it differ from rolling Windows?
- How do you test whether a time series is stationary, and why does stationarity matter for forecasting?
- What components does seasonal decomposition extract from a time series?
Answers:
- Resampling changes the observation frequency (e.g., daily to monthly). Rolling Windows compute statistics over a Sliding Window at the same frequency. Resampling reduces data points; rolling Windows smooth existing data.
- Use the Augmented Dickey-Fuller test. Stationarity matters because most forecasting models assume constant statistical properties over time. Non-stationary data must be differenced or transformed first.
- Seasonal decomposition extracts trend (long-term direction), seasonal (repeating patterns at fixed periods), and residual (random noise) components.
Challenge
Download 2 years of daily stock data for any major stock. Compute 20-day and 50-day rolling averages, identify crossovers, test for stationarity, decompose into trend and seasonality, and forecast the next 30 days using ARIMA.
FAQs
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro