To format Timedelta as ISO 8601, use the timedelta.isoformat() method. At first, import the required libraries −
import pandas as pd
Create a Timedelta object
timedelta = pd.Timedelta('4 days 11 hours 38 min 20 s 35 ms 55 ns')
Display the Timedelta
print("Timedelta...\n", timedelta)
Format as ISO 8601
timedelta.isoformat()
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('4 days 11 hours 38 min 20 s 35 ms 55 ns') # display the Timedelta print("Timedelta...\n", timedelta) # format as ISO 8601 res = timedelta.isoformat() # display the Timedelta as ISO 8601 print("\nFormat Timedelta as ISO 8601...\n", res)
Output
This will produce the following code
Timedelta... 4 days 11:38:20.035000055 Format Timedelta as ISO 8601... P4DT11H38M20.035000055S