Return NumPy Timedelta64 Object with ns Precision in Python Pandas



To return a numpy.timedelta64 object with ns precision, use the timedelta.to_timedelta64() method.

At first, import the required libraries −

import pandas as pd

Create a Timedelta object −

timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')

Display the Timedelta −

print("Timedelta...\n", timedelta)

Return a numpy.timedelta64 object with nanoseconds precision −

timedelta.to_timedelta64()

Example

Following is the code −

Open Compiler
import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') # display the Timedelta print("Timedelta...\n", timedelta) # Returns a numpy.timedelta64 object res = timedelta.to_timedelta64() # Returns a numpy.timedelta64 object with 'ns' precision print("\nA numpy.timedelta64 object with nanoseconds precision...\n", res)

Output

This will produce the following code −

Timedelta...
2 days 11:22:25.050000045

A numpy.timedelta64 object with nanoseconds precision...
213745050000045 nanoseconds
Updated on: 2021-10-14T06:41:05+05:30

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements