How to calculate difference between Dates in Python?
0

How to calculate difference between Dates in Python?

This recipe helps you calculate difference between Dates in Python
In [1]:
## How to claculate difference between Dates in Python 
def Kickstarter_Example_42():
    print()
    print(format('How to claculate difference between Dates in Python',
                 '*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # Load library
    import pandas as pd

    # Create data frame
    df = pd.DataFrame()

    # Create two datetime features
    df['Date1'] = [pd.Timestamp('01-01-2017'),
                     pd.Timestamp('01-04-2017')]
    df['Date2']    = [pd.Timestamp('01-01-2017'),
                     pd.Timestamp('01-06-2017')]

    # Calculate Difference (Method 1)
    print(df['Date2'] - df['Date1'])

    # Calculate Difference (Method 2)
    print(pd.Series(delta.days for delta in (df['Date2'] - df['Date1'])))

Kickstarter_Example_42()
***************How to claculate difference between Dates in Python****************
0   0 days
1   2 days
dtype: timedelta64[ns]
0    0
1    2
dtype: int64