Introduction
The python module datetime enables effortless manipulation and handling of dates, making it a convenient tool for working with date-related tasks. With the help of the datetime module, users can easily add or subtract a certain number of days from a given date.
In this tutorial, we will focus on how to add or subtract a certain number of days to a given date in Python.
Creating a datetime object
To start with, we will first import the necessary modules. In this case, we need to import the datetime module:
import datetime
Next, we need to specify the given date that we want to manipulate. We can do this by creating a datetime object with the required date information. For example, let's say we have the date 2019-08-03 and we want to subtract 2 days from it. We can create a datetime object using the following code:
date = datetime.date(2019, 8, 3)
Please note that when we check the type of date, we obtain a datetime.date object, as shown below:
type(date)
Output
datetime.date
Using timedelta function from the datetime module
Now that we have our date object, we can use the timedelta function from the datetime module to add or subtract a specific number of days. The timedelta function takes in three arguments: days, seconds and microseconds. For our case, we only need to specify the number of days that we want to add or subtract.
Subtracting n days from a specified date
To subtract 10 days from our specified date, we use the following code:
new_date = date - datetime.timedelta(days=10)
This will return a new datetime object with the date:
datetime.date(2019, 7, 24)
Here's another example showcasing the current date using the now() function.
current_date = datetime.datetime.now()
Output
datetime.datetime(2024, 1, 7, 8, 58, 55, 1885)
To subtract 10 days from now:
current_date - datetime.timedelta(days=10)
Ouput
datetime.datetime(2023, 12, 28, 8, 58, 55, 1885)
Adding n days from a specified date
Similarly, if we want to add 10 days to our specified date, we can use the plus sign (+) instead of the minus sign (-).
new_date = date + datetime.timedelta(days=10)
This will return a new datetime object with the date:
datetime.date(2019, 8, 13)
Adding and Subtracting Days using Numpy
Apart from using timedelta, we can also use the numpy module to add or subtract a specific number of days from a given date. This is useful when working with arrays of dates. To do this, we first need to convert our date object into a numpy array using the np.array function.
Creating an array with dates
In numpy, there are various ways to create an array of dates. If you have a single date, you can use the following syntax:
from numpy import timedelta64
import numpy as np
d = np.datetime64('2019-08-03')
For storing multiple dates in a numpy array, you can use the following syntax:
D = np.array(['2019-07-23', '2019-08-03', '2019-08-29'], dtype='datetime64[D]')
Having created the array of dates, let's now move on to adding or subtracting a certain number of days from a given date.
Subtracting Days with Numpy
To subtract days from a given date, we simply need to specify the number of days as an argument to the timedelta object. For example, if we want to add 10 days to a given date, we can use the following syntax:
d - timedelta64(10, 'D')
Output
numpy.datetime64('2019-07-24')
And
D - timedelta64(10, 'D')
Output
array(['2019-07-13', '2019-07-24', '2019-08-19'], dtype='datetime64[D]')
Adding Days with Numpy
Similarly, if we want to subtract 5 days from a given date, we can use this syntax:
D + timedelta64(10, 'D')
Output
array(['2019-08-02', '2019-08-13', '2019-09-08'], dtype='datetime64[D]')
Working with Different Units
In the example above, we used 'D' as our unit for adding or subtracting days. However, Numpy allows us to specify different units such as weeks, months, or even years. Let's take a look at another example:
Substracting or adding weeks
D - timedelta64(2, 'W')
Ouput
array(['2019-07-09', '2019-07-20', '2019-08-15'], dtype='datetime64[D]')
And
D + timedelta64(2, 'W')
Ouput
array(['2019-08-06', '2019-08-17', '2019-09-12'], dtype='datetime64[D]')
Substracting or adding months
To subtract or add months without encountering the error message
UFuncTypeError: ufunc 'add' cannot use operands with types dtype('O') and dtype('<m8[W]')
ou need to modify how you create your array. Begin by changing it to:
D = np.array(['2019-07-23','2019-08-03','2019-08-29'], dtype='datetime64[M]')
After that, you can proceed with the desired operations.
D - timedelta64(2, 'M')
The output will be:
array(['2019-05', '2019-06', '2019-06'], dtype='datetime64[M]')
Alternatively, for adding 2 months, employ:
D + timedelta64(2, 'M')
The output will be:
array(['2019-09', '2019-10', '2019-10'], dtype='datetime64[M]')
References
Links | Site |
---|---|
datetime | docs.python.org |
timedelta | docs.python.org |
now | docs.python.org |
datetime64 | numpy.org |