Add Month to datetime in Python

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.

Output:

2022-08-10 00:00:00

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,

Output:

2022-08-10 16:20:15

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.

Output:

2022-07-08 15:20:15

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,

Output:

2022-07-09T03:44:35.343283+05:30

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.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 June

    Fill Array with Random Numbers in Python

    Table of ContentsFill array with random numbers in PythonWays to fill array with random numbers in PythonUsing the numpy.random.randint() function to fill array with random numbers in PythonUsing the numpy.random.Generator.integers() function to fill array with random numbers in PythonUsing the random.randint() function to fill array with random numbers in PythonConclusionWas this post helpful? Fill array […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Python append to array
    30 December

    How to Append to Array in Python

    Table of ContentsWhat are the Different Array Implementations in Python?Python ListsPython ArraysPython Numpy ArraysAppend Element to List in pythonAppend to array in PythonAppend to NumPy array in pythonConclusionWas this post helpful? In python, we have three implementations of the array data structure. In this article, we will discuss those array implementations. After that, see how […]

  • Python array size
    17 December

    Python array size: get size of array in Python

    Table of ContentsPython array sizeWays to calculate the size of array in PythonUsing the len() functionUsing the numpy.size() attributeUsing the numpy.shape() attributeConclusionWas this post helpful? In this article, we will see how to get size of array in Python. Python array size An array stores a collection of similar elements in a contiguous memory location. […]

  • Python float array to int
    11 November

    Convert float array to int array in Python

    Table of ContentsWhat are the types of arrays?Ways to convert float arrays to int in PythonUsing the numpy.asarray() functionUsing the numpy.astype() functionUsing the numpy.int_() functionUsing the numpy.round_() functionUsing the numpy.rint() functionUsing the numpy.floor() functionUsing the numpy.ceil() functionUsing the numpy.trunc() functionConclusionWas this post helpful? What are the types of arrays? An array is a common structure […]

  • Convert Bytearray to String in Python
    01 November

    Convert Bytearray to String in Python

    Table of ContentsHow to create a bytearray from a string?Using the str() Function to convert Bytearray to String in PythonUsing the decode() Function to convert Bytearray to String in PythonUsing the codecs Module to convert Bytearray to String in PythonConclusionWas this post helpful? Python supports different types of sequence objects to store data. One such […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.