Introduction to NumPy datetime64
As we all know, Python is one of the most commonly used web programming languages. It is said that it is one of the easiest languages to learn for both the newbies and experts because it provides a lot of built-in functions that are easily accessible and can be used by the programmers. A large number of representations of date, time, and timespan are available in different Python packages for the programmer to import it into the program and use it according to the specific requirement. The datetime64 function allows the array of dates to be stored and printed in a compact form. It basically encodes the date in 64-bit integers. In this topic, we are going to learn about NumPy datetime64.
The datetime64 function in python allows the array representation of dates to the user. It takes the input in a particular format. Below given is the basic syntax of using the datetime64 function in a Python program:
Output generated by the datetime64 function is in ‘yyyy-mm-dd’ format.
How does datetime64 work in NumPy?
Let us understand the working of datetime64 in NumPy using the examples:
Basic output of datetime64 function in the Python program.
import numpy as npy var = npy.datetime64('2020-12-09') print var
In the above code, the basic working of the datetime64 function is shown with the input format is ‘YYYY-MM-DD’, and the date is displayed in the same format as the output.
Forcing to display the day unit from the input date.
import numpy as npy var = npy.datetime64('2020-12', 'D') print var
In the above code, when we do not specify the date unit (D) in the input date of the datetime64 function and ask for it to display on the console, it will automatically consider the first day of the month as the date.
Similarly, we can also use the other date units like ‘Y’, ‘M’ to display the output on the console accordingly.
Using the year ‘Y’ parameter of the date unit.
import numpy as npy var = npy.datetime64('2020-12', 'Y') print var
Using the month ‘M’ parameter of the date unit.
import numpy as npy var = npy.datetime64('2020-12', 'M') print var
Using the date and time both as an input of datetime64 function in the Python program.
import numpy as npy var = npy.datetime64('2020-12-04 12:00:30') print var
In the above code, we have specified both the date and time as the input parameters of the datetime64 function keeping the space between the date and time, but please observe the output displayed. In the output, ‘T’ is displayed in between both of them.
We can use the date and time in both the formats given below:
- Date and time together separated by space.
- Date and time together with ‘T’ in between
In both cases, the output will be the same, i.e. python will insert ‘T’ in between them.
Let’s make it clear with an example:
import numpy as npy var = npy.datetime64('2020-12-04T12:00:30') print var
In both cases, the output is displayed the same by python.
Extracting hours from the input date unit only in the datetime64 function.
import numpy as npy date = npy.datetime64('2020-12-04') hours = npy.datetime64(date, 'h') print hours
In the above code, the time unit is not mentioned; if the user tries to extract the hours from it, it will display as ‘00’ with the ‘T’ in between the date and time.
Similarly, we can use the ‘m’, ‘s’ to extract the minute and seconds from the date-time unit.
Extracting minutes and seconds from the date and time unit using the datetime64 function.
import numpy as npy date = npy.datetime64('2020-12-04T12:03:05') minutes = npy.datetime64(date, 'm') print minutes seconds = npy.datetime64(date, 's') print seconds
Arranging the datetime unit in an array.
import numpy as npy dates= npy.array(['2020-12-04T12:03:05','2020-01-08T12:05:05', '2020-02-12T05:03:05', '2020-06-04T11:03:05'], dtype='datetime64') print dates
In the above code, we have created an array of the datetime units. It basically takes 2 arguments, i.e. datetime and the ‘dtype’.
Using the arange function in datetime64 function
We can also use functions like ‘arange’ in order to display the range of dates between the 2 dates input in the datetime64 function of python. Let’s understand it with the help of an example.
import numpy as npy dates= npy.arange('2020-12-01','2020-12-15', dtype='datetime64') print dates
In the above code, all the dates get displayed on the console that lies between the two given dates, i.e. ‘2020-12-01’ to ‘2020-12-15’.
Performing the arithmetic operations on datetime units.
We can perform the basic arithmetic operations on datetime units using the arithmetic operators like (+, -). For example, we can subtract 2 dates to find the number of days in between, adding the days in a date given and subtracting the months from the particular date. Let’s understand them in with the help of an example:
Subtracting the days in the input date of datetime64 function:
import numpy as npy dates= npy.datetime64('2020-12') print dates+5
In the above code, as the date ‘D’ unit is not mentioned, it will consider it as 01 by default. So adding ‘5’ in it will display the date as ‘2020-12-06.’
Subtracting the 2 dates to calculate the days in between
import numpy as npy dates_between= npy.datetime64('2020-12-08')- npy.datetime64('2020-11-08') print dates_between
In the above code, 2 dates using the function datetime64 are subtracted, and the number of days in between them is evaluated, i.e. 30.
Adding the months in the input date unit of the datetime64 function.
import numpy as npy month = npy.datetime64('2020-12-08', 'M') months_final = month+4 print months_final
In the above code, months are first extracted from the date and then added by 4, which is stored in the variable ‘months_final’ and displayed on the console to the user.
The above description clearly explains the datetime64 function and why the programmers use it. Though there are a number of date and time functions available in Python in separate packages, it is very important to understand their relationship with each other. As the different date and time function gives the output in a specific format, so being a programmer, it is mandatory to understand each function properly before using them in a program.
This is a guide to NumPy datetime64. Here we discuss How does datetime64 works in NumPy and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –