How to calculate difference between Dates in Python?

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


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
2 developers from Infosys
1 developer from Ericsson
1 developer from LTI
1 developer from YASH Technologies
1 developer from Altimetrik
1 developer from HvH
1 developer from MudraCircle
1 developer from American Express
1 developer from Renovite Technologies
1 developer from ANAC