Python Pandas - Extract the Number of days for each element from TimeDeltaIndex

To extract the number of days for each element from a TimedeltaIndex object, use the TimedeltaIndex.days property. This property returns an Int64Index containing only the days component from each timedelta.

Creating a TimedeltaIndex

First, import pandas and create a TimedeltaIndex with various time duration formats ?

import pandas as pd

# Create a TimeDeltaIndex object with different time formats
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
                                  '2 day 4h 03:08:02.000045', '+21:15:45.999999'])

print("TimedeltaIndex...")
print(tdIndex)
TimedeltaIndex...
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
                '2 days 07:08:02.000045', '0 days 21:15:45.999999'],
               dtype='timedelta64[ns]', freq=None)

Extracting Days Using .days Property

Use the .days property to extract only the days component from each timedelta ?

import pandas as pd

tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
                                  '2 day 4h 03:08:02.000045', '+21:15:45.999999'])

# Extract number of days from each element
days_only = tdIndex.days
print("Number of days from each TimeDelta:")
print(days_only)
print(f"Type: {type(days_only)}")
Number of days from each TimeDelta:
Index([10, 0, 2, 0], dtype='int64')
Type: <class 'pandas.core.indexes.base.Index'>

Complete Example with Components

Here's a complete example showing days extraction alongside the full components breakdown ?

import pandas as pd

# Create TimedeltaIndex with various formats
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
                                  '2 day 4h 03:08:02.000045', '+21:15:45.999999'])

print("Original TimedeltaIndex:")
print(tdIndex)

print("\nDays only:")
print(tdIndex.days)

print("\nAll components breakdown:")
print(tdIndex.components)
Original TimedeltaIndex:
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
                '2 days 07:08:02.000045', '0 days 21:15:45.999999'],
               dtype='timedelta64[ns]', freq=None)

Days only:
Index([10, 0, 2, 0], dtype='int64')

All components breakdown:
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0    10      5        2        0             0             3           10
1     0     22       39       19           999           999            0
2     2      7        8        2             0            45            0
3     0     21       15       45           999           999            0

Key Points

  • The .days property returns only the days component, ignoring hours, minutes, and smaller units
  • Time durations less than 24 hours return 0 days
  • The result is an Index object with int64 dtype
  • Use .components to see all time components in a DataFrame format

Conclusion

The TimedeltaIndex.days property provides a simple way to extract just the days component from timedelta objects. Use this when you only need the day count and want to ignore other time components like hours or minutes.

Updated on: 2026-03-26T17:36:38+05:30

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements