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 minute of the hour component of the Period
In Pandas, you can extract the minute component from a Period object using the period.minute property. This property returns the minute value (0-59) from the Period's datetime representation.
Syntax
period.minute
Where period is a Pandas Period object containing datetime information.
Creating Period Objects
First, let's create Period objects with different datetime formats ?
import pandas as pd
# Create Period from string representation
period1 = pd.Period("2020-09-23 05:55:30")
# Create Period using individual components
period2 = pd.Period(freq="T", year=2021, month=7, day=16, hour=2, minute=35)
print("Period1:", period1)
print("Period2:", period2)
Period1: 2020-09-23 05:55:30 Period2: 2021-07-16 02:35
Extracting Minute Component
Use the minute property to get the minute value from Period objects ?
import pandas as pd
# Create Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="T", year=2021, month=7, day=16, hour=2, minute=35)
# Extract minute components
minute1 = period1.minute
minute2 = period2.minute
print(f"Minute from period1: {minute1}")
print(f"Minute from period2: {minute2}")
Minute from period1: 55 Minute from period2: 35
Working with Period Arrays
You can also extract minutes from multiple Period objects at once ?
import pandas as pd
# Create a PeriodIndex with multiple periods
periods = pd.PeriodIndex(['2020-01-15 10:25:45', '2021-06-30 14:18:12', '2022-12-05 23:59:03'], freq='S')
print("Periods:")
print(periods)
print("\nMinutes:")
print(periods.minute)
Periods: PeriodIndex(['2020-01-15 10:25:45', '2021-06-30 14:18:12', '2022-12-05 23:59:03'], dtype='period[S]') Minutes: Index([25, 18, 59], dtype='int64')
Conclusion
The period.minute property provides a simple way to extract minute components from Pandas Period objects. It works with both individual Period objects and PeriodIndex arrays, returning integer values from 0 to 59.
Advertisements
