w3resource

NumPy: Convert numpy datetime64 to Timestamp

NumPy DateTime: Exercise-7 with Solution

Write a NumPy program to convert numpy datetime64 to Timestamp.

Sample Solution:-

Python Code:

# Importing the required libraries
import numpy as np
from datetime import datetime

# Getting the current date and time in UTC
dt = datetime.utcnow()

# Displaying the current date and time
print("Current date:")
print(dt)

# Converting the datetime object to a NumPy datetime64 object
dt64 = np.datetime64(dt)

# Calculating the timestamp from the datetime64 object
# The timestamp is calculated by subtracting the datetime '1970-01-01T00:00:00Z' from the current datetime
# Then it's divided by the number of seconds in one unit of timedelta64 to get the timestamp
ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')

# Displaying the timestamp
print("Timestamp:")
print(ts)

# Converting the timestamp back to UTC datetime and displaying it
print("UTC from Timestamp:")
print(datetime.utcfromtimestamp(ts))

Sample Output:

Current date:                                                          
2017-04-01 08:01:12.722055                                             
Timestamp:                                                             
1491033672.72                                                          
UTC from Timestamp:                                                    
2017-04-01 08:01:12.722055                                             

Explanation:

In the above exercise –

dt = datetime.utcnow() - Get the current date and time using the datetime.utcnow() function and store it in the dt variable.

dt64 = np.datetime64(dt) - Convert the dt variable to a numpy datetime64 object using the np.datetime64() function and store it in the dt64 variable.

ts = (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's') - Calculate the number of seconds that have elapsed since the Unix epoch (January 1, 1970 at 00:00:00 UTC) by subtracting the Unix epoch datetime from dt64, dividing the result by a numpy timedelta64 object representing one second, and storing the result in the ts variable.

print(datetime.utcfromtimestamp(ts)) - Convert the ts variable to a human-readable datetime object in UTC timezone using the datetime.utcfromtimestamp() function and finally print the result.

Pictorial Presentation:

NumPy: Convert numpy datetime64 to Timestamp.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to find the number of weekdays in March 2017.
Next: Pandas Data Series, DataFrame Exercises Home.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/numpy/python-numpy-datetime-exercise-7.php