Python Pandas - Return the microseconds from Timedelta object using string input


To return the nanoseconds from Timedelta object, use the timedelta.microseconds property. At first, import the required libraries −

import pandas as pd

TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Set string input for microseconds using unit 'us'. Create a Timedelta object

timedelta = pd.Timedelta('12 min 40 us')

Display the Timedelta

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

Return the microseconds value

timedelta.microseconds

Example

Following is the code

import pandas as pd

# TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s
# set string input for microseconds using unit 'us'
# create a Timedelta object
timedelta = pd.Timedelta('12 min 40 us')

# Display the Timedelta:
print("Timedelta...\n", timedelta)

# return the microseconds value
res = timedelta.microseconds

# display the microseconds
print("\nTimedelta (microseconds value)...\n", res)

Output

This will produce the following code

Timedelta...
0 days 00:12:00.040000

Timedelta (microseconds value)...
40000

Updated on: 13-Oct-2021

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements