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

To extract the number of seconds for each element from TimeDeltaIndex object, use the TimedeltaIndex.seconds property. This property returns the seconds component (0-59) of each timedelta element.

Creating a TimeDeltaIndex

First, let's create a TimeDeltaIndex with various time intervals ?

import pandas as pd

# Create a TimeDeltaIndex object with different time intervals
tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 35s 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:35.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 Seconds Component

The seconds property returns the seconds component from each timedelta element ?

import pandas as pd

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

# Extract the seconds component
seconds = tdIndex.seconds
print("The number of seconds from the TimeDeltaIndex object...")
print(seconds)
The number of seconds from the TimeDeltaIndex object...
Int64Index([18155, 81559, 25682, 76545], dtype='int64')

Understanding the Results

The seconds values represent the total seconds within each day. Let's break down the components to understand better ?

import pandas as pd

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

# Display all components
print("Components breakdown:")
print(tdIndex.components)

print("\nSeconds component only:")
print(tdIndex.seconds)
Components breakdown:
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0    10      5        2       35             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

Seconds component only:
Int64Index([18155, 81559, 25682, 76545], dtype='int64')

Key Points

Important aspects of the seconds property:

  • Total seconds within day: Returns total seconds elapsed within each day (0-86399)
  • Excludes days: Days component is not included in the seconds calculation
  • Calculation: hours × 3600 + minutes × 60 + seconds
  • Return type: Returns an Int64Index object

Conclusion

The TimedeltaIndex.seconds property extracts the total seconds component within each day from timedelta objects. Use components to see all time components separately for better understanding of the breakdown.

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

231 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements