Recipe: How to deal with Rolling Time Window in Python?
DATA MUNGING DATA CLEANING PYTHON

How to deal with Rolling Time Window in Python?

This recipe helps you deal with Rolling Time Window in Python
In [1]:
## How to deal with Rolling Tine Window in Python 
def Kickstarter_Example_47():
    print()
    print(format('How to deal with Rolling Time Window in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load library
    import pandas as pd

    # Create datetimes
    time_index = pd.date_range('01/01/2010', periods=5, freq='M')

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

    # Create feature
    df['Stock_Price'] = [1,2,3,4,5]
    print(); print(df)

    # Create A Rolling Time Window Of Two Rows
    # Calculate rolling mean
    df1 = df.rolling(window=2).mean()
    print(); print(df1)

    # Identify max value in rolling time window
    df2 = df.rolling(window=2).max()
    print(); print(df2)

Kickstarter_Example_47()
******************How to deal with Rolling Time Window in Python******************

            Stock_Price
2010-01-31            1
2010-02-28            2
2010-03-31            3
2010-04-30            4
2010-05-31            5

            Stock_Price
2010-01-31          NaN
2010-02-28          1.5
2010-03-31          2.5
2010-04-30          3.5
2010-05-31          4.5

            Stock_Price
2010-01-31          NaN
2010-02-28          2.0
2010-03-31          3.0
2010-04-30          4.0
2010-05-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
1 developer from HvH
1 developer from MudraCircle
1 developer from Altimetrik
1 developer from IBM
1 developer from Renovite Technologies
1 developer from ANAC
1 developer from KPMG
1 developer from Vodafone
1 developer from Candela Labs
1 developer from LTI