Convert a pandas Timedelta object into a Python timedelta object

To convert a pandas Timedelta object into a Python timedelta object, use the to_pytimedelta() method. This is useful when you need to work with Python's standard library datetime module.

Syntax

timedelta_obj.to_pytimedelta()

Note: Any nanosecond resolution will be lost during conversion, as Python's datetime.timedelta only supports microsecond precision.

Creating a Pandas Timedelta Object

First, let's create a pandas Timedelta object with various time components ?

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 original Timedelta
print("Original Pandas Timedelta:")
print(timedelta)
print("Type:", type(timedelta))
Original Pandas Timedelta:
2 days 11:22:25.050000045
Type: <class 'pandas._libs.tslibs.timedeltas.Timedelta'>

Converting to Python timedelta

Now convert the pandas Timedelta to Python's standard timedelta object ?

import pandas as pd
from datetime import timedelta as dt_timedelta

# Create a pandas Timedelta object
pd_timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns')

# Convert to Python timedelta
py_timedelta = pd_timedelta.to_pytimedelta()

print("Pandas Timedelta:", pd_timedelta)
print("Python timedelta:", py_timedelta)
print("Type:", type(py_timedelta))
Pandas Timedelta: 2 days 11:22:25.050000045
Python timedelta: 2 days, 11:22:25.050000
Type: <class 'datetime.timedelta'>

Precision Loss Example

Notice how nanoseconds are lost during conversion ?

import pandas as pd

# Create Timedelta with nanoseconds
td_with_ns = pd.Timedelta('1 second 123456789 nanoseconds')
print("Original (with nanoseconds):", td_with_ns)

# Convert to Python timedelta (loses nanoseconds)
py_td = td_with_ns.to_pytimedelta()
print("Converted (microseconds only):", py_td)

# Show the difference
print("Nanoseconds lost:", td_with_ns.nanoseconds % 1000)
Original (with nanoseconds): 0 days 00:00:01.123456789
Converted (microseconds only): 0:00:01.123457
Nanoseconds lost: 789

Practical Use Case

This conversion is useful when working with Python's datetime arithmetic ?

import pandas as pd
from datetime import datetime

# Create a pandas Timedelta
pd_delta = pd.Timedelta('3 days 5 hours')

# Convert to Python timedelta for datetime arithmetic
py_delta = pd_delta.to_pytimedelta()

# Use with Python datetime
start_time = datetime(2024, 1, 1, 10, 0, 0)
end_time = start_time + py_delta

print("Start time:", start_time)
print("Time delta:", py_delta)
print("End time:", end_time)
Start time: 2024-01-01 10:00:00
Time delta: 3 days, 5:00:00
End time: 2024-01-04 15:00:00

Conclusion

Use to_pytimedelta() to convert pandas Timedelta objects to Python's standard timedelta for compatibility with datetime operations. Be aware that nanosecond precision will be lost during conversion.

Updated on: 2026-03-26T16:24:07+05:30

769 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements