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
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
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
todayargument specifies the current date. - The
Dargument specifies resolution of one day.
Example 3: Use datetime64() For Different Time Units
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,
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:
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,
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.
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.
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.