w3resource

NumPy: Convert numpy datetime64 to Timestamp


7. Convert np.datetime64 to Timestamp

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.

For more Practice: Solve these Related Problems:

  • Write a function that converts a numpy.datetime64 object to a POSIX timestamp (seconds since epoch) using .astype('M8[s]').
  • Create a solution that converts a numpy.datetime64 array into Python datetime objects and then extracts their Unix timestamps.
  • Implement a program that performs a round-trip conversion: from np.datetime64 to timestamp and back to np.datetime64, verifying consistency.
  • Test the conversion on a series of dates and compare the results with those obtained from the time module in Python.

Go to:


Previous: Write a NumPy program to find the number of weekdays in March 2017.
Next: NumPy String Exercises Home.

Python-Numpy Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.