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
Python Pandas - Extract the minute from DateTimeIndex with specific time series frequency
To extract the minute from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.minute property. This property returns an Int64Index containing the minute values for each timestamp in the DateTimeIndex.
Syntax
DateTimeIndex.minute
Creating a DateTimeIndex
First, let's create a DateTimeIndex with a specific frequency. We'll use period 6 and frequency as 'T' (minute) with Australia/Sydney timezone ?
import pandas as pd
# Create DatetimeIndex with period 6 and frequency as T i.e. minute
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:55', periods=6, tz='Australia/Sydney', freq='T')
# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:55+11:00', '2021-10-20 02:31:55+11:00',
'2021-10-20 02:32:55+11:00', '2021-10-20 02:33:55+11:00',
'2021-10-20 02:34:55+11:00', '2021-10-20 02:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='T')
DateTimeIndex frequency...
<Minute>
Extracting Minutes
Now we can extract the minute component from each timestamp using the minute property ?
import pandas as pd
# Create DatetimeIndex with period 6 and frequency as T i.e. minute
# The timezone is Australia/Sydney
datetimeindex = pd.date_range('2021-10-20 02:30:55', periods=6, tz='Australia/Sydney', freq='T')
# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
# Get the minute component
print("\nExtracting the minute values...")
minutes = datetimeindex.minute
print(minutes)
print(f"Type: {type(minutes)}")
DateTimeIndex...
DatetimeIndex(['2021-10-20 02:30:55+11:00', '2021-10-20 02:31:55+11:00',
'2021-10-20 02:32:55+11:00', '2021-10-20 02:33:55+11:00',
'2021-10-20 02:34:55+11:00', '2021-10-20 02:35:55+11:00'],
dtype='datetime64[ns, Australia/Sydney]', freq='T')
Extracting the minute values...
Index([30, 31, 32, 33, 34, 35], dtype='int64')
Type: <class 'pandas.core.indexes.numeric.Int64Index'>
Different Time Series Frequencies
You can use different frequencies and still extract minutes. Here's an example with hourly frequency ?
import pandas as pd
# Create DatetimeIndex with hourly frequency
hourly_index = pd.date_range('2021-10-20 02:15:30', periods=4, freq='H')
print("Hourly DateTimeIndex:")
print(hourly_index)
print("\nMinutes from hourly frequency:")
print(hourly_index.minute)
Hourly DateTimeIndex:
DatetimeIndex(['2021-10-20 02:15:30', '2021-10-20 03:15:30',
'2021-10-20 04:15:30', '2021-10-20 05:15:30'],
dtype='datetime64[ns]', freq='H')
Minutes from hourly frequency:
Index([15, 15, 15, 15], dtype='int64')
Key Points
- The
minuteproperty returns values from 0 to 59 - Returns an Int64Index containing the minute values
- Works with any time series frequency
- Preserves timezone information in the original DateTimeIndex
Conclusion
The DateTimeIndex.minute property provides an efficient way to extract minute components from datetime indices. It works with any time series frequency and returns an Int64Index with values ranging from 0 to 59.
