Python Pandas - Get the month of the year component of the Period

To get the month of the year component of the Period, use the period.month property. The Pandas Period represents a period of time and provides various properties to extract date components.

Syntax

period.month

Creating Period Objects

First, let's create Period objects using different methods ?

import pandas as pd

# Creating Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="M", year=2021, month=8, day=16, hour=2, minute=35)

# Display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

The output of the above code is ?

Period1...
 2020-09-23 05:55:30
Period2...
 2021-08

Extracting Month Component

Use the month property to extract the month number from Period objects ?

import pandas as pd

# Creating Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="M", year=2021, month=8, day=16, hour=2, minute=35)

# Get the month of the year from Period objects
month1 = period1.month
month2 = period2.month

# Display the results
print("Month from the 1st Period object:", month1)
print("Month from the 2nd Period object:", month2)

The output of the above code is ?

Month from the 1st Period object: 9
Month from the 2nd Period object: 8

Multiple Period Objects

You can also work with multiple periods at once ?

import pandas as pd

# Creating multiple Period objects
periods = [
    pd.Period("2023-01-15"),
    pd.Period("2023-06-30"),
    pd.Period("2023-12-25")
]

# Extract months from all periods
months = [period.month for period in periods]

print("Periods:", periods)
print("Months:", months)

The output of the above code is ?

Periods: [Period('2023-01-15', 'D'), Period('2023-06-30', 'D'), Period('2023-12-25', 'D')]
Months: [1, 6, 12]

Conclusion

The period.month property provides a simple way to extract the month component from Pandas Period objects. It returns an integer value from 1 to 12 representing the month of the year.

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

367 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements