NumPy provides functionality for working with dates and times.
The datetime64()
function in Numpy stores date and time information as a 64-bit integer datetime64
object.
Example 1: Get Current Date and Time in NumPy
import numpy as np
# get the current date and time
result = np.datetime64('now')
print("Current date and time:")
print(result)
Current date and time: 2023-04-29T04:00:05
In this example, we have used the datetime64()
function with the now
argument to get the current date and time.
The output above indicates that the current date and time is April 29th, 2023, at 4:00:05 AM.
Example 2: Get Current Date in NumPy
import numpy as np
# get the current date
date_today = np.datetime64('today', 'D')
print("Today's date:")
print(date_today)
Output
Today's date: 2023-04-29
Here, we have used the datetime64()
function with the today
and D
argument to get the current date.
- The
today
argument specifies the current date. - The
D
argument specifies resolution of one day.
Example 3: Use datetime64() For Different Time Units
import numpy as np
# use datetime64() for different time units
year = np.datetime64('2023', 'Y')
month = np.datetime64('2023-04', 'M')
day = np.datetime64('2023-04-29', 'D')
hour = np.datetime64('2023-04-29T10', 'h')
minute = np.datetime64('2023-04-29T10:30', 'm')
second = np.datetime64('2023-04-29T10:30:15', 's')
print("Year: ", year)
print("Month: ", month)
print("Day: ", day)
print("Hour: ", hour)
print("Minute: ", minute)
print("Second: ", second)
Output
Year: 2023 Month: 2023-04 Day: 2023-04-29 Hour: 2023-04-29T10 Minute: 2023-04-29T10:30 Second: 2023-04-29T10:30:15
In the above example, we have used the datetime64()
to create the datetime64
objects for different time units.
Note: Even though it is not strictly necessary to specify the time unit, it's a good practice to specify them while creating the datetime64
objects.
Convert datetime64 Objects
In NumPy, it is possible to convert datetime64
objects to and from other data types.
1. Convert datetime64 to Python datetime Object
We can convert the datetime64
object to Python's datetime object. For example,
import numpy as np
from datetime import datetime
# create a datetime64 object
dt64 = np.datetime64('2023-04-29T12:34:56')
# convert datetime64 to datetime object
dt = dt64.astype(datetime)
# print the datetime object
print(dt)
Output
2023-04-29 12:34:56
2. Convert Python datetime Object to datetime64
Here's how we can convert Python's datetime
object to the datetime64
object:
import numpy as np
from datetime import datetime
# create a datetime object
dt = datetime(2023, 4, 29, 12, 34, 56)
# convert datetime to datetime64 object
dt64 = np.datetime64(dt)
# print the datetime64 object
print(dt64)
Output
2023-04-29T12:34:56
Create a Range of Dates
In NumPy, we use the arange()
function to create a range of dates. For example,
import numpy as np
# create a range of dates from 2023-04-01 to 2023-04-10
dates = np.arange('2023-04-01', '2023-04-11', dtype='datetime64[D]')
# print the dates
print(dates)
Output
['2023-04-01' '2023-04-02' '2023-04-03' '2023-04-04' '2023-04-05' '2023-04-06' '2023-04-07' '2023-04-08' '2023-04-09' '2023-04-10']
In this example, we have used the arrange()
function to create a range of dates from April 1st, 2023 to April 10th, 2023.
Here, dtype='datetime64[D]'
indicates that each date in the range should have a resolution of one day.
Arithmetic Operations on NumPy datetime64 Objects
We can perform arithmetic operations like addition, subtraction on NumPy's datetime64
objects.
Let's see an example.
import numpy as np
# create a datetime64 object for today
today = np.datetime64('today')
# add one day to today's date
tomorrow = today + np.timedelta64(1, 'D')
# create datetime64 objects for two dates
date1 = np.datetime64('2023-05-01')
date2 = np.datetime64('2023-04-01')
# calculate the number of days between the two dates
num_days = date1 - date2
# display the results
print("Today's date:", today)
print("Tomorrow's date:", tomorrow)
print("Number of days between", date1, "and", date2, "is", num_days)
Output
Today's date: 2023-04-29 Tomorrow's date: 2023-04-30 Number of days between 2023-05-01 and 2023-04-01 is 30 days
In the above example, we have used np.datetime64('today')
to create the datetime64
object for today's date.
Then we added one day to today's date using today + np.timedelta64(1, 'D')
to get tomorrow's date.
Notice the code,
date1 - date2
Here, we have performed an arithmetic subtraction operation to calculate the number of days between the two dates.
Note: timedelta()
is a Python function that is part of the datetime
module. To learn more, visit Python datetime.timedelta Class.
NumPy busday() Function
In NumPy, the np.busday()
function is used to calculate the number of business days (i.e., weekdays excluding holidays) between two dates.
Let's see an example.
import numpy as np
# create datetime64 objects for two dates
date1 = np.datetime64('2023-04-01')
date2 = np.datetime64('2023-05-01')
# calculate the number of business days between the two dates
num_business_days = np.busday_count(date1, date2)
# display the number of business days between the two dates
print("Number of business days between", date1, "and", date2, "is", num_business_days)
Output
Number of business days between 2023-04-01 and 2023-05-01 is 20
Here, we have used np.busday_count()
to calculate the number of business days between date1 and date2.