Add months to a date in Pandas Add months to a date in Pandas pandas pandas

Add months to a date in Pandas


You could use pd.DateOffset

In [1756]: df.date + pd.DateOffset(months=plus_month_period)Out[1756]:0   2017-01-111   2017-02-01Name: date, dtype: datetime64[ns]

Another way using pd.offsets.MonthOffset

In [1785]: df.date + pd.offsets.MonthOffset(plus_month_period)Out[1785]:0   2016-10-141   2016-11-04Name: date, dtype: datetime64[ns]

Details

In [1757]: dfOut[1757]:        date0 2016-10-111 2016-11-01In [1758]: plus_month_periodOut[1758]: 3


Suppose you have a dataframe of the following format, where you have to add integer months to a date column.

Start_DateMonths_to_add
2014-06-0123
2014-06-014
2000-10-0110
2016-07-013
2017-12-0190
2019-01-012

In such a scenario, using Zero's code or mattblack's code won't be useful. You have to use lambda function over the rows where the function takes 2 arguments -

  1. A date to which months need to be added to
  2. A month value in integer format

You can use the following function:

# Importing required modulesfrom dateutil.relativedelta import relativedelta# Defining the functiondef add_months(start_date, delta_period):  end_date = start_date + relativedelta(months=delta_period)  return end_date

After this you can use the following code snippet to add months to the Start_Date column. Use progress_apply functionality of Pandas. Refer to this Stackoverflow answer on progress_apply : Progress indicator during pandas operations.

from tqdm import tqdmtqdm.pandas()df["End_Date"] = df.progress_apply(lambda row: add_months(row["Start_Date"], row["Months_to_add"]), axis = 1)

Here's the full code form dataset creation, for your reference:

import pandas as pdfrom dateutil.relativedelta import relativedeltafrom tqdm import tqdmtqdm.pandas()# Initilize a new dataframedf = pd.DataFrame()# Add Start Date columndf["Start_Date"] = ['2014-06-01T00:00:00.000000000',                    '2014-06-01T00:00:00.000000000',                    '2000-10-01T00:00:00.000000000',                    '2016-07-01T00:00:00.000000000',                    '2017-12-01T00:00:00.000000000',                    '2019-01-01T00:00:00.000000000']# To convert the date column to a datetime formatdf["Start_Date"] = pd.to_datetime(df["Start_Date"])# Add months columndf["Months_to_add"] = [23, 4, 10, 3, 90, 2]# Defining the Add Months functiondef add_months(start_date, delta_period):  end_date = start_date + relativedelta(months=delta_period)  return end_date# Apply function on the dataframe using lambda operation.df["End_Date"] = df.progress_apply(lambda row: add_months(row["Start_Date"], row["Months_to_add"]), axis = 1)

You will have the final output dataframe as follows.

Start_DateMonths_to_addEnd_Date
2014-06-01232016-05-01
2014-06-0142014-10-01
2000-10-01102001-08-01
2016-07-0132016-10-01
2017-12-01902025-06-01
2019-01-0122019-03-01

Please add to comments if there are any issues with the above code.
All the best!