Python Pandas - Get the second component of the Period

In Pandas, a Period object represents a specific time period. To extract the second component from a Period object, use the period.second property, which returns an integer value (0-59) representing the seconds.

Creating Period Objects

You can create Period objects in multiple ways ?

import pandas as pd

# Create Period from datetime string
period1 = pd.Period("2020-09-23 05:55:30")

# Create Period using individual components
period2 = pd.Period(freq="S", year=2021, month=7, day=16, hour=2, minute=35, second=10)

print("Period1:", period1)
print("Period2:", period2)
Period1: 2020-09-23 05:55:30
Period2: 2021-07-16 02:35:10

Extracting the Second Component

Use the .second property to get the second component from Period objects ?

import pandas as pd

# Create Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="S", year=2021, month=7, day=16, hour=2, minute=35, second=10)

# Extract second component
seconds1 = period1.second
seconds2 = period2.second

print("Seconds from Period1:", seconds1)
print("Seconds from Period2:", seconds2)
Seconds from Period1: 30
Seconds from Period2: 10

Multiple Period Examples

Here's how to extract seconds from multiple Period objects ?

import pandas as pd

# Create a list of Period objects
periods = [
    pd.Period("2023-01-15 08:25:45"),
    pd.Period("2023-02-20 14:30:12"),
    pd.Period("2023-03-10 22:15:03")
]

# Extract seconds from all periods
for i, period in enumerate(periods, 1):
    print(f"Period {i}: {period} ? Seconds: {period.second}")
Period 1: 2023-01-15 08:25:45 ? Seconds: 45
Period 2: 2023-02-20 14:30:12 ? Seconds: 12
Period 3: 2023-03-10 22:15:03 ? Seconds: 3

Conclusion

The .second property provides a simple way to extract the second component from Pandas Period objects. This property returns an integer value between 0 and 59, making it useful for time-based analysis and filtering operations.

Updated on: 2026-03-26T16:29:35+05:30

142 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements