
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Convert a pandas Timedelta object into a Python timedelta object
To convert a pandas Timedelta object into a Python timedelta object, use the timedelta.to_pytimedelta() 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 a pandas Timedelta object into a python timedelta object. Any nanosecond resolution will be lost −
timedelta.to_pytimedelta()
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 a pandas Timedelta object into a python timedelta object res = timedelta.to_pytimedelta() # display the converted Timedelta object # Any nanosecond resolution will be lost print("\nTimedelta object after converting...\n", res)
Output
This will produce the following code −
Timedelta... 2 days 11:22:25.050000045 Timedelta object after converting... 2 days, 11:22:25.050000
- Related Articles
- Python Pandas - Return the microseconds from Timedelta object
- Python Pandas - Return the nanoseconds from Timedelta object
- Python Pandas - Return the seconds from Timedelta object
- Python Pandas - Return the maximum value of the Timedelta object
- Python Pandas - Return the minimum value of the Timedelta object
- Python Pandas - Convert the Timedelta to a NumPy timedelta64
- Python Pandas - Return the nanoseconds from Timedelta object using integer input
- Python Pandas - Return the nanoseconds from Timedelta object using string input
- Python Pandas - Return the microseconds from Timedelta object using integer input
- Python Pandas - Return the microseconds from Timedelta object using string input
- Python Pandas - Get the seconds from Timedelta object using integer input
- Python Pandas - Get the seconds from Timedelta object using string input
- Python Pandas - Get the Total seconds in the duration from the Timedelta object
- Python Pandas - Format Timedelta as ISO 8601
- Python Pandas - Return a new Timedelta with milliseconds ceiling resolution

Advertisements