Skip to Content

Pandas – Add Months to Date

In this tutorial, we’ll try to solve 2 problems:

  1. Add months to a date in a specific cell of a pandas dataframe.
  2. Add months to all the cells in a pandas dataframe (datetime type) column.

Datatype conversion:

Before going through the below solutions for the problem, we first need to make sure that the datatype of the date_column for which we need to add months, has to be pandas datetime. In case you have strings in the column then try to use pandas.to_datetime method to convert the datatype of the column to pandas datetime.

code:

df['date_column'] = pd.to_datetime(df['date_column'])

The above line of code changes the datatype of the ‘date_column’ column to pandas datetime.

DateOffset function:
Pandas module has a DateOffset(months,...) function that adds the specified number of months to the date. This function is used as a solution for the below problem.

Syntax:

pandas.DateOffset(months)

Parameters:
months : The number of months to be added to the given date.
Result:
The resultant date after adding the months specified to the given date.

Problem – 1: Add months to date in a specific cell of dataframe

Here, we’re trying to add months to the date in a specific cell of a pandas dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once you make sure the datatype of the column is pandas datetime, then select the specific cell to which you need to add the months.
  3. Then add the months using the pd.DateOffset(months) function over the selected cell which gives the resultant date of the specified cell after adding the specified number of months.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'][0]+pd.DateOffset(months=10)

In the above code, we first convert the data type of the column to pandas datetime and then add 10 months to the date of the specified cell using the pd.DateOffset(months=10) method.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the below examples.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2019']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas dataframe with a datetime column

Example – 1

code:

df['date_column'][0]+pd.DateOffset(months=10)

Output:

Timestamp('2018-03-14 00:00:00')

Example – 2

code:

df['date_column'][2]+pd.DateOffset(months=50)

Output:

Timestamp('2023-11-07 00:00:00')

Problem – 2: Add months to all the cells in the column of a dataframe

add months to date in a pandas dataframe

Here, we’re trying to add months to all the cells in the column of a pandas dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once you make sure the datatype of the column is pandas datetime, then select the specific column to which you need to add the months.
  3. Then add the months using pd.DateOffset(months) function over the selected column which gives the resultant dates of the specified column after adding months.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column']+pd.DateOffset(months=30)

In the above code, we first convert the data type of the column to pandas datetime and then add 30 months to all dates of the specified column using the .pd.DateOffset(months=30) method.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the below examples.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017', '2017', '07 - 09 - 2019']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas dataframe with a datetime column

Example – 1

code:

df['date_column']+pd.DateOffset(months=30)

Output:

0   2019-11-14
1   2019-07-01
2   2022-03-07
Name: date_column, dtype: datetime64[ns]

Summary

In this we looked at 2 different problems and tried to solve each one separately

  1. Add months to the date in a specific cell of a pandas dataframe.

    Solution: Using pd.DateOffset(months) method

    Result: Date after specific number of months

  2. Add months to all the cells of a pandas dataframe datetime type column.

    Solution: Using pd.DateOffset(months) method

    Result: Series of dates of all cells from the specific column after adding specific number of months.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.