Python Pandas - Extract the seconds from the DateTimeIndex with specific time series frequency

To extract the seconds from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.second property. This property returns an Int64Index containing the second component of each datetime in the index.

Syntax

DateTimeIndex.second

Creating a DateTimeIndex

First, let's create a DateTimeIndex with a seconds frequency to demonstrate the extraction ?

import pandas as pd

# Create DatetimeIndex with period 6 and frequency as S (seconds)
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='S')

# Display DateTimeIndex
print("DateTimeIndex...\n", datetimeindex)
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:50+11:00', '2021-10-20 02:30:51+11:00',
               '2021-10-20 02:30:52+11:00', '2021-10-20 02:30:53+11:00',
               '2021-10-20 02:30:54+11:00', '2021-10-20 02:30:55+11:00'],
              dtype='datetime64[ns, Australia/Sydney]', freq='S')

Extracting Seconds

Use the second property to extract seconds from each datetime ?

import pandas as pd

# Create DatetimeIndex with seconds frequency
datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='S')

# Display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# Get the seconds
print("\nExtracting the seconds...\n", datetimeindex.second)
DateTimeIndex frequency...
<Second>

Extracting the seconds...
Int64Index([50, 51, 52, 53, 54, 55], dtype='int64')

Different Time Series Frequencies

The second property works with any DateTimeIndex regardless of its frequency ?

import pandas as pd

# Create DateTimeIndex with minute frequency
minute_index = pd.date_range('2021-10-20 02:30:15', periods=3, freq='T')
print("Minute frequency DateTimeIndex:")
print(minute_index)
print("Seconds:", minute_index.second)

print("\n" + "="*50 + "\n")

# Create DateTimeIndex with hour frequency  
hour_index = pd.date_range('2021-10-20 02:30:45', periods=3, freq='H')
print("Hour frequency DateTimeIndex:")
print(hour_index)
print("Seconds:", hour_index.second)
Minute frequency DateTimeIndex:
DatetimeIndex(['2021-10-20 02:30:15', '2021-10-20 02:31:15',
               '2021-10-20 02:32:15'],
              dtype='datetime64[ns]', freq='T')
Seconds: Int64Index([15, 15, 15], dtype='int64')

==================================================

Hour frequency DateTimeIndex:
DatetimeIndex(['2021-10-20 02:30:45', '2021-10-20 03:30:45',
               '2021-10-20 04:30:45'],
              dtype='datetime64[ns]', freq='H')
Seconds: Int64Index([45, 45, 45], dtype='int64')

Conclusion

The DateTimeIndex.second property extracts the second component (0-59) from datetime values, returning an Int64Index. It works with any time series frequency and preserves the original timezone information.

Updated on: 2026-03-26T17:05:02+05:30

580 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements