Difference between two date columns in pandas can be achieved using timedelta function in pandas. In this section we will be covering difference between two dates in days, week , and year in pandas python with example for each. We will be explaining how to get

- Difference between two dates in days pandas dataframe python
- Difference between two dates in weeks pandas dataframe python
- Difference between two dates in Months pandas dataframe python
- Difference between two dates in years pandas dataframe python

First lets create a dataframe with two dates.

import pandas as pd import numpy as np import datetime from dateutil.relativedelta import relativedelta from datetime import date date1 = pd.Series(pd.date_range('2012-1-1 12:00:00', periods=7, freq='M')) date2 = pd.Series(pd.date_range('2013-3-11 21:45:00', periods=7, freq='W')) df = pd.DataFrame(dict(Start_date = date1, End_date = date2)) print(df)

so the resultant dataframe will be

**Difference between two dates in days – pandas dataframe python**

- First line calculates the difference between two dates
- Second line converts the difference in terms of days (timedelta64(1,’D’)- D indicates days)

df['diff_days'] = df['End_date'] - df['Start_date'] df['diff_days']=df['diff_days']/np.timedelta64(1,'D') print(df)

so the resultant dataframe will be

**Difference between two dates in weeks – pandas dataframe python**

- First line calculates the difference between two dates
- Second line converts the difference in terms of weeks (timedelta64(1,’W’)- W indicates weeks)

df['diff_weeks'] = df['End_date'] - df['Start_date'] df['diff_weeks']=df['diff_weeks']/np.timedelta64(1,'W') print(df)

so the resultant dataframe will be

** **

**Difference between two dates in Months – pandas dataframe python**

- First line calculates the difference between two dates
- Second line converts the difference in terms of Months (timedelta64(1,’M’)- capital M indicates Months)

df['diff_months'] = df['End_date'] - df['Start_date'] df['diff_months']=df['diff_months']/np.timedelta64(1,'M') print(df)

so the resultant dataframe will be

**Difference between two dates in Years – pandas dataframe python**

- First line calculates the difference between two dates
- Second line converts the difference in terms of Years (timedelta64(1,’Y’)- Y indicates years)

df['diff_years'] = df['End_date'] - df['Start_date'] df['diff_years']=df['diff_years']/np.timedelta64(1,'Y') print(df)

so the resultant dataframe will be