Python Pandas - Get the number of days from TimeDelta

To get the number of days from a TimeDelta object in Pandas, use the timedelta.days property. This property returns only the day component as an integer, excluding hours, minutes, and seconds.

Syntax

timedelta.days

Creating a TimeDelta Object

First, create a TimeDelta object using pd.Timedelta() ?

import pandas as pd

# Create a Timedelta object
timedelta = pd.Timedelta('5 days 1 min 45 s')
print("Timedelta:", timedelta)
Timedelta: 5 days 00:01:45

Extracting Days

Use the .days property to get only the number of days ?

import pandas as pd

# Create a Timedelta object
timedelta = pd.Timedelta('5 days 1 min 45 s')

# Get number of days
days = timedelta.days
print("Number of days:", days)
print("Type:", type(days))
Number of days: 5
Type: <class 'int'>

Multiple Examples

Here are different ways to create TimeDelta objects and extract days ?

import pandas as pd

# Different TimeDelta formats
td1 = pd.Timedelta('10 days')
td2 = pd.Timedelta(days=7, hours=12, minutes=30)
td3 = pd.Timedelta('2 days 23:59:59')

print("TimeDelta 1:", td1, "? Days:", td1.days)
print("TimeDelta 2:", td2, "? Days:", td2.days)  
print("TimeDelta 3:", td3, "? Days:", td3.days)
TimeDelta 1: 10 days 00:00:00 ? Days: 10
TimeDelta 2: 7 days 12:30:00 ? Days: 7
TimeDelta 3: 2 days 23:59:59 ? Days: 2

Key Points

  • The .days property returns an integer, not a float
  • Only the complete days are returned (hours/minutes/seconds are ignored)
  • For partial days, use .total_seconds() / 86400 to get fractional days

Getting Fractional Days

To include fractional parts, convert total seconds to days ?

import pandas as pd

td = pd.Timedelta('2 days 12 hours')

print("Complete days only:", td.days)
print("Total days (fractional):", td.total_seconds() / 86400)
Complete days only: 2
Total days (fractional): 2.5

Conclusion

Use timedelta.days to extract only complete days as an integer. For fractional days including hours and minutes, use total_seconds() / 86400 instead.

Updated on: 2026-03-26T16:00:56+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements