Extract Minute from DatetimeIndex in Python Pandas



To extract the minute from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.minute property.

At first, import the required libraries −

import pandas as pd

Create a 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...\n", datetimeindex)

Get the minute −

print("\nGetting the minute..\n",datetimeindex.minute)

Example

Following is the code −

import pandas as pd

# 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...\n", datetimeindex)

# display DateTimeIndex frequency
print("DateTimeIndex frequency...\n", datetimeindex.freq)

# get the minute
print("\nGetting the minute..\n",datetimeindex.minute)

Output

This will produce the following code −

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>

Getting the minute..
Int64Index([30, 31, 32, 33, 34, 35], dtype='int64')
Updated on: 2021-10-18T12:22:28+05:30

992 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements