Python Pandas - Get the seconds of the period from the PeriodIndex object

To get the seconds of the period from the PeriodIndex object, use the PeriodIndex.second property. This extracts the second component from each period in the index.

What is PeriodIndex?

PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time. It's useful for representing time periods with specific frequencies.

Creating a PeriodIndex

First, let's create a PeriodIndex object with datetime strings ?

import pandas as pd

# Create a PeriodIndex object with second frequency
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
                             '2021-07-15 02:55:15', '2022-06-25 09:40:55'], freq="S")

print("PeriodIndex...")
print(periodIndex)
PeriodIndex...
PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45', '2021-07-15 02:55:15', '2022-06-25 09:40:55'], dtype='period[S]')

Extracting Seconds

Use the second property to extract the second component from each period ?

import pandas as pd

periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
                             '2021-07-15 02:55:15', '2022-06-25 09:40:55'], freq="S")

# Extract seconds from the PeriodIndex object
seconds = periodIndex.second
print("The seconds from the PeriodIndex object...")
print(seconds)
The seconds from the PeriodIndex object...
Index([35, 45, 15, 55], dtype='int64')

Complete Example

Here's a comprehensive example showing PeriodIndex creation and second extraction ?

import pandas as pd

# Create a PeriodIndex object
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
                             '2021-07-15 02:55:15', '2022-06-25 09:40:55'], freq="S")

# Display PeriodIndex object
print("PeriodIndex...")
print(periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency object...")
print(periodIndex.freq)

# Display PeriodIndex frequency as string
print("\nPeriodIndex frequency object as a string...")
print(periodIndex.freqstr)

# Display seconds from the PeriodIndex object
print("\nThe seconds from the PeriodIndex object...")
print(periodIndex.second)
PeriodIndex...
PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45', '2021-07-15 02:55:15', '2022-06-25 09:40:55'], dtype='period[S]')

PeriodIndex frequency object...
<Second>

PeriodIndex frequency object as a string...
S

The seconds from the PeriodIndex object...
Index([35, 45, 15, 55], dtype='int64')

Key Points

  • The second property returns an Index object containing the second values
  • Values range from 0 to 59 representing seconds in a minute
  • The frequency parameter "S" indicates second-level precision

Conclusion

The PeriodIndex.second property efficiently extracts second components from time periods. This is useful for time-based analysis and filtering operations in pandas.

Updated on: 2026-03-26T18:02:31+05:30

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements