Pandas - Number of Months Between Two Dates - GeeksforGeeks

18 Dec, 2021
In this article, we are going to find the number of months between two dates in pandas using Python. 
Example 1:
We will take a dataframe and have two columns for the dates between which we want to get the difference. Use df.dates1-df.dates2 to find the difference between the two dates and then convert the result in the form of months. Convert into ‘int’ datatype else result will be in form of float.
Python3
# Importing required librariesimport pandas as pdimport numpy as npimport datetime # Making a dataframe which will have two# columns two store different datesdf = pd.DataFrame({'dates1': np.array(  [datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),                   'dates2': np.array(                     [datetime.datetime(1998, 6, 20),                      datetime.datetime(2012, 10, 18)])}) # Used to convert the difference in terms of monthsdf['nb_months'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'M'))df['nb_months'] = df['nb_months'].astype(int)print(df)
Output:
Example 2:
We can also get the number of days between two dates by doing a slight modification in the code. It is shown as follows:
Python3
# Importing required librariesimport pandas as pdimport numpy as npimport datetime # Making a dataframe which will have# two columns two store different datesdf = pd.DataFrame({'dates1': np.array(  [datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),                   'dates2': np.array(                     [datetime.datetime(1998, 6, 20),                      datetime.datetime(2012, 10, 18)])}) # Used to convert the difference in terms of daysdf['Number_of_days'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'D'))df['Number_of_days'] = df['Number_of_days'].astype(int)print(df)
Output:
Example 3:
In a similar way, we can get differences between the two dates in terms of weeks also.
Python3
# Importing required librariesimport pandas as pdimport numpy as npimport datetime # Making a dataframe which will# have two columns two store different datesdf= pd.DataFrame({'dates1': np.array(  [datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),                   'dates2': np.array(                     [datetime.datetime(1998, 6, 20),                      datetime.datetime(2012, 10, 18)] )}) # Used to convert the difference in terms of weeksdf['Number_of_weeks'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'W'))df['Number_of_weeks'] = df['Number_of_weeks'].astype(int)print(df)
Output:
Example 4:
In a similar way, we can get differences between the two dates in terms of years also.
Python3
# Importing required librariesimport pandas as pdimport numpy as npimport datetime # Making a dataframe which will# have two columns two store different datesdf = pd.DataFrame({'dates1': np.array(  [datetime.datetime(2000, 10, 19), datetime.datetime(2021, 1, 8)]),                   'dates2': np.array(                     [datetime.datetime(1998, 6, 20),                      datetime.datetime(2012, 10, 18)])}) # Used to convert the difference in terms of yearsdf['Number_of_years'] = ((df.dates1 - df.dates2)/np.timedelta64(1, 'Y'))df['Number_of_years'] = df['Number_of_years'].astype(int)print(df)
Output:
sagartomar9927
Picked
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python Classes and Objects
How to drop one or multiple columns in Pandas Dataframe
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Create a directory in Python
Python | Pandas dataframe.groupby()