Table of Contents
In Python, we can represent date values using the standard datetime
library. We can store all the attributes of the date along with time attributes in datetime
objects. The datetime
objects can be manipulated with other libraries also like dateutil
, pandas
, and more.
We cannot directly add or subtract datetime
objects and use the datetime.timedelta
object to represent such intervals of time for an addition. However, this object cannot represent intervals of months, so we need to look for alternative methods.
Ways to add month to datetime
in Python
Different methods on how to add months to datetime
in Python are discussed below.
Using the datetime.datetime
constructor to add month to datetime
in Python.
The datetime.datetime
constructor is used to initializing datetime
objects in Python. This constructor accepts the month and other attributes for the date and time values.
We cannot directly overwrite such objects. We can just initialize the values at first and access them.
However, we can create our own function to create a new datetime
object with updated values. We will use the values from the defined object and perform some calculations to create a new object.
See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
from datetime import datetime def add_mon(d,x): dom = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] n_month = ((( d.month - 1) + x ) % 12 ) + 1 newyear = d.year + ((( d.month - 1) + x ) // 12 ) if d.day > dom[n_month-1]: n_day = dom[n_month-1] else: n_day = d.day return datetime( newyear, n_month, n_day) d = datetime(2022,6,10,16,20,15) d_new = add_mon(d, 2) print(d_new) |
Output:
In the above example, the add_mon()
function creates a new datetime
object after adding the months to a give datetime
object.
Using the dateutil.relativedelta
object to add month to datetime
in Python
The dateutil
module was developed to increase the functionalities of the standard datetime
module. We can use functions from this module to work with datetime
objects.
This module provides a relativedelta
object to represent intervals of time, similar to the timedelta
object of the datetime
module. The difference between the two is that the former accepts a wide range of attributes (including months) and the latter has very few attributes.
Due to this, the relativedelta
object can represent different intervals of time. We can use it to add months to datetime
in Python.
For example,
1 2 3 4 5 6 |
from datetime import datetime from dateutil.relativedelta import relativedelta d = datetime(2022,6,10,16,20,15) + relativedelta(months = 2) print(d) |
Output:
Using the pandas.DateOffset()
function to add month to datetime
in Python
The pandas
library is used to work with DataFrame objects in Python. Columns of DataFrame can contain timestamp
values that represent date and time values.
Due to this, the pandas
library also has some basic functionalities for working with such values. We can use the pandas.DateOffset()
function to add months to datetime
in Python.
This function is used to create time intervals. With the months
attribute, we can create intervals of months and add them to any given datetime
object.
See the code below.
1 2 3 4 5 6 7 |
from datetime import datetime import pandas as pd d1 = datetime(2022,5,8,15,20,15) d2 = d1 + pd.DateOffset(months = 2) print(d2) |
Output:
In the above example, we adds an interval of 2 months using the pandas.DateOffset()
function.
Using the arrow.shift()
function to add month to datetime
in Python
arrow
was introduced as a third-party package that combines the functionalities of datetime
and dateutil
into one and provides easy-to-use functionalities.
This module has its own objects that can be used to represent date and time values. The shift()
function can be used to update such objects by adding or subtracting intervals of time. We can use the months
attribute in this function to add months to the object.
For example,
1 2 3 4 5 6 |
import arrow t = arrow.now() new_t = t.shift(months=2) print(new_t) |
Output:
Conclusion
In this tutorial, we discussed several methods to add months to datetime
in Python. The datetime
object cannot be overwritten but we can use the values from this object to create a new object after adding the months. The timedelta
object will not work in this case. The relativedelta
object from the dateutil
module can be used to add months to datetime
in Python. Similarly, we can use the pandas.DateOffset()
function as well. The arrow
module is also discussed since it has a direct function to update its object and add months to it.