Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Get the month number of the period from the PeriodIndex object
To get the month number from a PeriodIndex object in Pandas, use the month property. This returns month numbers where January=1, February=2, ..., December=12.
Syntax
PeriodIndex.month
Creating a PeriodIndex Object
First, let's create a PeriodIndex object with different dates ?
import pandas as pd
# Create a PeriodIndex object with minute 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="T")
print("PeriodIndex object:")
print(periodIndex)
PeriodIndex object: PeriodIndex(['2021-09-25 07:30', '2019-10-30 04:15', '2021-07-15 02:55', '2022-06-25 09:40'], dtype='period[T]')
Getting Month Numbers
Use the month property to extract month numbers from the PeriodIndex ?
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="T")
# Get month numbers
months = periodIndex.month
print("Month numbers:")
print(months)
print(f"\nData type: {type(months)}")
Month numbers: Index([9, 10, 7, 6], dtype='int64') Data type: <class 'pandas.core.indexes.base.Index'>
Complete Example
Here's a comprehensive example showing PeriodIndex properties and month 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="T")
# 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 month number from the PeriodIndex object
print("\nThe month number from the PeriodIndex object...")
print(periodIndex.month)
PeriodIndex... PeriodIndex(['2021-09-25 07:30', '2019-10-30 04:15', '2021-07-15 02:55', '2022-06-25 09:40'], dtype='period[T]') PeriodIndex frequency object... <Minute> PeriodIndex frequency object as a string... T The month number from the PeriodIndex object... Index([9, 10, 7, 6], dtype='int64')
Key Points
- The
monthproperty returns an Index object containing integer month numbers - Month numbering follows the standard: January=1, February=2, ..., December=12
- The frequency parameter ("T" for minute) doesn't affect month extraction
- You can access individual month numbers using standard indexing:
periodIndex.month[0]
Conclusion
The PeriodIndex.month property provides an efficient way to extract month numbers from period data. It returns an Index object containing integers from 1-12 representing the months in your time series data.
Advertisements
