Python

# NumPy datetime64

One of the simplest and easiest to learn among all the programming languages is Python Programming language. Python provides various built-in functions that allow you to write the code a lot easier if you are an expert or a beginner. Today, we will talk about one built-in function from the Python language, i.e., “datetime64”. This function represents the date and time and displays them in a more intact and compact form. Using this function, we can import any representation of the date, time, and timespan into our program and use them per our needs. NumPy is the library package that Python provides to allow the declarations of the nd-arrays and to apply different operations on the multi-dimensional arrays and the matrices.

## Procedure

This guide will inform its readers about the various parameters we can use to generate the different outputs in terms of date, time, and timestamp using the datetime64 method. The guide first gives a brief description of the usage and working of the DateTime module and then displays the syntax of the different examples of this function.

## Syntax

The datetime64 can be used to give different representations of the date and the time format. To utilize this function, we must know about its calling method. We may call this function by simply copying and specifying the parameters following the syntax below:

\$ numpy.datetime64 (‘dates’)

The datetime64 function takes the input, then generates the time and date with the month, day, and year information.

## Return Value

If we follow the previous basic syntax of the datetime64 represented in the Python script, the function will generate the date output in the format “yyyy-mm-dd”.

## Example 1

We will use the basic method of the datetime64 () function to generate the basic output from this function. The program for this example will be written in Python’s “spyder” open-source environment. After successfully creating a new project in the Python shell, we will install and import the necessary modules from the python packages. The one basic library we will use with the datetime64 method is “Numpy”. We will use this module by importing it as “pn”.

To write the program for this example, we will only use datetime64 to generate the date and time. We will call the method “pn.datetime64 (‘date’)”. To the input parameter of the function, we will pass the date value in the format, e.g., “yyyy-mm-dd”. This format will take the year, month, and date, so we will give it the value “2023-10-03”. Then we will save the results in some variable and print that variable to display the results from the function on the screen. The program for this example is rewritten in the python language, and it can be copied from the following snippet of the code to see the output resulting from the function call of the datetime64 () method.

#importing numpy module
import numpy as pn
# declaring the date representation using datetime64
date = pn.datetime64('2023-10-03')
#displaying the output
print (date)  When we build the program copied from the snippet given above, we get the output as the date with the format that we had specified in the parameter of the function datetime64.

## Example 2

Let us try some more examples to understand the working of this function to enable us to utilize the full format of the function by modifying its input parameters for the different representations of the date and the time. In this example, we will implement this function in such a way that it will find the date units from the function itself. So, for example, if we follow the format “yyyy-mm-dd” and pass the year, month, and date to the function “pn.datetime64 ()” and want the function to only return the year from the date, this can be done by introducing another date unit in the parameter of the function.

So, if we want to know the year from the date, we would pass the additional parameter “Y” to the datetime64 () method as “pn.datetime64 (‘2023-12-24’, ‘Y’)” then the function will return only the year in the output from the parameter date that we had specified to it. Following the same procedure, if we replace the “Y” with the “M”, the function will return the month in the output. The results from this function call are mentioned in the following figure:

#importing numpy module
import numpy as pn
# declaring the year from the date representation using datetime64
date = pn.datetime64('2023-12-24','Y')
#displaying the output
print (date) ## Example 3

Previous examples have discussed displaying the date, year, and month using the datetime64 function, so this example will show how we can say the timestamp and, from the time stamp, the information of hours, minutes, and even the seconds using the function datetime64. To do so, we will import the NumPy as “np”, and then using np, we will call the method “np.datetime64 (‘dateTtime’)” to display the timestamp with the date in the output. This function takes the date and time with the “T” placed between them as its parameter.

If we display the results from this function, call the output would have both the date and time. If we want to display the date, timestamp, and the information of, let’s say a minute or a second, then we call the datetime64 method “np.datetime64 (‘2020-12-09T12:04:06’, ‘m’)”. Here, the “m” represents the time unit for the minute. We can use the “s” instead of ‘m’, and the s will then represent the time unit of the second. We will save this function in a variable and print the results by calling the print () function. The code for this example is given in the following snippet:

#importing numpy module
import numpy as pn
# declaring the minute from the date and time representation using datetime64
date = pn.datetime64('2020-12-09T12:04:06', ‘m’)
#displaying the output
print (date) The function has returned as its output the date, timestamp, and time information in the form of minutes.

## Conclusion

The article is based on the topic “np. datetime64 ()” method. The article has depicted the method to implement the different ways to represent the date and time using the datetime64 () with the help of the three various examples, each dealing with the modified parameter of the function datetime64() so that the users could get clarity on this function and its parameters without ambiguities. 