How can I add and subtract months from a date in Pandas?

How can I add and subtract months from a date in Pandas?

Pandas is a powerful data analysis library in Python that offers various tools and functions for working with dates and time. To add or subtract months from a date in Pandas, one can use the “pd.DateOffset” function which allows for the manipulation of dates by a specified number of months. This can be achieved by specifying the number of months to add or subtract in the “months” parameter, along with the desired date in the “date” parameter. This function returns a new date value with the requested adjustment. Additionally, Pandas provides other useful functions such as “pd.tseries.offsets.MonthEnd” which can be used to get the last day of the month after adding or subtracting months. This feature makes Pandas a convenient tool for performing date arithmetic in various data analysis tasks.

Add and Subtract Months from a Date in Pandas


You can use the following methods to add and subtract months from a date in pandas:

Method 1: Add Months to Date

from pandas.tseries.offsetsimport DateOffset
df['date_column'] + DateOffset(months=3)

Method 2: Subtract Months from Date

from pandas.tseries.offsetsimport DateOffset
df['date_column'] - DateOffset(months=3)

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': pd.date_range(start='1/5/2022', freq='M', periods=10),
                   'sales': [6, 8, 9, 5, 4, 8, 8, 3, 5, 9]})

#view DataFrame
print(df)

        date  sales
0 2022-01-31      6
1 2022-02-28      8
2 2022-03-31      9
3 2022-04-30      5
4 2022-05-31      4
5 2022-06-30      8
6 2022-07-31      8
7 2022-08-31      3
8 2022-09-30      5
9 2022-10-31      9

Example 1: Add Months to Date in Pandas

The following code shows how to create a new column that adds 3 months to the value in the date column:

from pandas.tseries.offsetsimport DateOffset

#create new column that adds 3 months to date
df['date_plus3'] = df.date + DateOffset(months=3)

#view updated DataFrame
print(df)

        date  sales date_plus3
0 2022-01-31      6 2022-04-30
1 2022-02-28      8 2022-05-28
2 2022-03-31      9 2022-06-30
3 2022-04-30      5 2022-07-30
4 2022-05-31      4 2022-08-31
5 2022-06-30      8 2022-09-30
6 2022-07-31      8 2022-10-31
7 2022-08-31      3 2022-11-30
8 2022-09-30      5 2022-12-30
9 2022-10-31      9 2023-01-31

The new column date_plus3 represents the values in the date column with three months added to each value.

Example 2: Subtract Months from Date in Pandas

The following code shows how to create a new column that subtracts 3 months to the value in the date column:

from pandas.tseries.offsetsimport DateOffset

#create new column that subtracts 3 months from date
df['date_minus3'] = df.date + DateOffset(months=3)

#view updated DataFrame
print(df)

        date  sales date_minus3
0 2022-01-31      6  2021-10-31
1 2022-02-28      8  2021-11-28
2 2022-03-31      9  2021-12-31
3 2022-04-30      5  2022-01-30
4 2022-05-31      4  2022-02-28
5 2022-06-30      8  2022-03-30
6 2022-07-31      8  2022-04-30
7 2022-08-31      3  2022-05-31
8 2022-09-30      5  2022-06-30
9 2022-10-31      9  2022-07-31

The new column date_minus3 represents the values in the date column with three months subtracted from each value.

The following tutorials explain how to perform other common operations in pandas:

Cite this article

stats writer (2024). How can I add and subtract months from a date in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-and-subtract-months-from-a-date-in-pandas/

stats writer. "How can I add and subtract months from a date in Pandas?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-and-subtract-months-from-a-date-in-pandas/.

stats writer. "How can I add and subtract months from a date in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-and-subtract-months-from-a-date-in-pandas/.

stats writer (2024) 'How can I add and subtract months from a date in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-and-subtract-months-from-a-date-in-pandas/.

[1] stats writer, "How can I add and subtract months from a date in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I add and subtract months from a date in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top