Python Pandas - Convert the Timedelta to a NumPy timedelta64


To convert the Timedelta to a NumPy timedelta64, 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)

Convert the Timedelta to a NumPy timedelta64 −

timedelta.to_timedelta64()

Example

Following is the code −

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)

# Convert the Timedelta to a NumPy timedelta64.
res = timedelta.to_timedelta64()

# Return the result
print("\nConverting the Timedelta to a NumPy timedelta64....\n", res)

Output

This will produce the following code −

Timedelta...
2 days 11:22:25.050000045

Converting the Timedelta to a NumPy timedelta64....
213745050000045 nanoseconds

Updated on: 14-Oct-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements