Recipe: How to deal with missing values in a Timeseries in Python?
DATA MUNGING PANDAS SERIES

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

This recipe helps you deal with missing values in a Timeseries in Python
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


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
3 developers from Infosys
1 developer from Harman
1 developer from LTI
1 developer from YASH Technologies
1 developer from Altimetrik
1 developer from HvH
1 developer from MudraCircle
1 developer from ANAC
1 developer from Renovite Technologies
1 developer from Embicon