How to deal with missing values in a Timeseries in Python?
DATA MUNGING

How to deal with missing values in a Timeseries in Python?

How to deal with missing values in a Timeseries in Python?

This recipe helps you deal with missing values in a Timeseries in Python

0
This python code does the following: 1. Creates necessary Time-Series dataframe for manipulation. 2.Uses different techniques for filling the missing values. 3.Uses Interpolation method to fill missing values. 4.Forward fill and backward fill in time series.
In [1]:
## How to deal with missing values in a Timeseries in Python 
def Kickstarter_Example_45():
    print()
    print(format('How to deal with missing values in a Timeseries in Python',
                 '*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load libraries
    import pandas as pd
    import numpy as np

    # Create date
    time_index = pd.date_range('28/03/2017', periods=5, freq='M')

    # Create data frame, set index
    df = pd.DataFrame(index=time_index);
    print(df)

    # Create feature with a gap of missing values
    df['Sales'] = [1.0,2.0,np.nan,np.nan,5.0];
    print(); print(df)

    # Interpolate missing values
    df1= df.interpolate();
    print(); print(df1)

    # Forward-fill Missing Values
    df2 = df.ffill();
    print(); print(df2)

    # Backfill Missing Values
    df3 = df.bfill();
    print(); print(df3)

    # Interpolate Missing Values But Only Up One Value
    df4 = df.interpolate(limit=1, limit_direction='forward');
    print(); print(df4)

    # Interpolate Missing Values But Only Up Two Values
    df5 = df.interpolate(limit=2, limit_direction='forward');
    print(); print(df5)

Kickstarter_Example_45()
************How to deal with missing values in a Timeseries in Python*************
Empty DataFrame
Columns: []
Index: [2017-03-31 00:00:00, 2017-04-30 00:00:00, 2017-05-31 00:00:00, 2017-06-30 00:00:00, 2017-07-31 00:00:00]

            Sales
2017-03-31    1.0
2017-04-30    2.0
2017-05-31    NaN
2017-06-30    NaN
2017-07-31    5.0

            Sales
2017-03-31    1.0
2017-04-30    2.0
2017-05-31    3.0
2017-06-30    4.0
2017-07-31    5.0

            Sales
2017-03-31    1.0
2017-04-30    2.0
2017-05-31    2.0
2017-06-30    2.0
2017-07-31    5.0

            Sales
2017-03-31    1.0
2017-04-30    2.0
2017-05-31    5.0
2017-06-30    5.0
2017-07-31    5.0

            Sales
2017-03-31    1.0
2017-04-30    2.0
2017-05-31    3.0
2017-06-30    NaN
2017-07-31    5.0

            Sales
2017-03-31    1.0
2017-04-30    2.0
2017-05-31    3.0
2017-06-30    4.0
2017-07-31    5.0

Relevant Projects

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Data Science Project-All State Insurance Claims Severity Prediction
Data science project in R to develop automated methods for predicting the cost and severity of insurance claims.