Converting NumPy datetime64 to Timestamp

The datetime64 format can be complicated to understand in many situations. Therefore, there is a need to convert NumPy datetime64 into a timestamp format in Python. So we can easily represent the DateTime in Hours, Minutes, and Seconds format. Thus in this tutorial, we will learn how to convert NumPy datetime64 to Timestamp using the following mentioned steps.

Importing the libraries

Initially, we will import the DateTime and NumPy libraries of Python. The DateTime module in Python provides various classes to represent date and time since there are no such datatypes available. It provides several functions to manipulate the date and time in Python.

from datetime import datetime
import numpy as np

Extract the current date in UTC Format in Python

Next, we shall use the utcnow() from the DateTime module. This function will simply return the current date and time in UTC Format. The UTC format is represented as YYYY-DD-MM HH:MM: SS. Finally, we print the current date and time in UTC format.

curr_date = datetime.utcnow()
print("Current date is:",curr_date)


Current date is: 2022-10-07 09:12:19.329861

Convert current date to datetime64 format

We shall then convert the current date to datetime64 format using the datetime64 function of the DateTime module. The datetime64 function gives a compact format of an array of dates. Encoding of date in 64-bit integer format happens using this function.

date64 = np.datetime64(curr_date)

Creating Timestamp from datetime64 format in Python

We will convert the date64 object created above into seconds, minutes, and hours. Firstly, we will create a datetime64 object of the standard UTC epoch time. We will subtract this value from the date64 object. Then we will divide this resultant value by the timedelta64 function. In order to subtract two datetime64 values, Python introduced another package called deltatime64 to complement the DateTime module. It takes two arguments. First is a number representing the number of units and a date/time unit such as seconds(‘s’), minutes(‘m’) or hours(‘h’). The difference was divided with deltatime64(1,’s’) ┬áto get the difference in seconds.

We repeat these steps to find minutes and hours. Lastly, we print the timestamp in form of seconds, minutes, and hours respectively.

seconds= (date64 - np.datetime64('1970-01-01T00:00:00Z'))/ np.timedelta64(1, 's')
print("Datetime in Timestamp in seconds:", seconds)
minutes= (date64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 'm')
print("Datetime in Timestamp in minutes:", minutes)
hours= (date64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 'h')
print("Datetime in Timestamp in hours:", hours)


Datetime in Timestamp in seconds: 1665134725.37592
Datetime in Timestamp in minutes: 27752245.422932
Datetime in Timestamp in hours: 462537.42371553334

Thus we have reached the end of this tutorial. To learn more about Numpy DateTime click on the following link: How to create NumPy linspace out of datetime

Leave a Reply

Your email address will not be published. Required fields are marked *