Data Science : Time Series Analysis using Python

Ashita Saxena
Analytics Vidhya
Published in
4 min readJul 1, 2020

What is a Time Series?

Time series is a sequence of observations recorded at regular time intervals.

Depending on the frequency of observations, a time series may typically be hourly, daily, weekly, monthly, quarterly and annual. Sometimes, you might have seconds and minute-wise time series as well, like, number of clicks and user visits every minute etc.

Need of Time Series:

Time Series is needed as it is the preparatory step before you develop a forecast of the series.

Besides, time series forecasting has enormous commercial significance because stuff that is important to a business like demand and sales, number of visitors to a website, stock price etc are essentially time series data.

Importing Time Series in Python:

Import the necessary libraries:

from dateutil.parser import parse 
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd

Uploading the .csv file:

Performing the various operations on time series:

  • Time Series Indexing
  • Detrending a Time Series
  • Additive and Multiplicative Time Series
  • White Noise in Time Series
  • Auto and Partial correlation function in Time Series
  • Test for Stationarity in Time Series

1- INDEXING WITH TIME SERIES DATA →

Visualizing Time Series:

2- Detrending a Time Series →

Detrending a time series is to remove the trend component from a time series.

3- Additive and Multiplicative Time Series →

Depending on the nature of the trend and seasonality, a time series can be modeled as an additive or multiplicative, wherein, each observation in the series can be expressed as either a sum or a product of the components:

Additive time series:
Value = Base Level + Trend + Seasonality + Error

Multiplicative Time Series:
Value = Base Level x Trend x Seasonality x Error

OUTPUT:

4- White Noise in Time Series →

Like a stationary series, the white noise is also not a function of time, that is its mean and variance does not change over time. But the difference is, the white noise is completely random with a mean of 0. Mathematically, a sequence of completely random numbers with mean zero is a white noise.

5- Auto and Partial correlation function in Time Series →

Autocorrelation is simply the correlation of a series with its own lags. If a series is significantly autocorrelated, that means, the previous values of the series (lags) may be helpful in predicting the current value.

Partial Autocorrelation also conveys similar information but it conveys the pure correlation of a series and its lag, excluding the correlation contributions from the intermediate lags.

OUTPUT:

6- Test for Stationarity in Time Series →

Stationarity is a property of a time series. A stationary series is one where the values of the series is not a function of time.

“If you are not willing to risk the usual, you will have to settle for the ordinary.”

- Jim Rohn

THANK YOU!!

KEEP LEARNING😊✌

--

--