Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Return Index Locations of Values at Particular Time of Day in DateTimeIndex



To return index locations of values at particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_at_time() method.

At first, import the required libraries −

import pandas as pd

Create DatetimeIndex with period 5 and frequency as T i.e. minutes −

datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')

Display DateTimeIndex −

print("DateTimeIndex...\n", datetimeindex)

Display index locations of values at particular time of day i.e. 03:10:50 here −

print("\nIndex locations of values at particular time of day...\n",
datetimeindex.indexer_at_time('2021-10-30 03:10:50'))

Example

Following is the code −

import pandas as pd

# DatetimeIndex with period 5 and frequency as T i.e. minutes
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')

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

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

# display index locations of values at particular time of day i.e. 03:10:50 here
print("\nIndex locations of values at particular time of day...\n",
datetimeindex.indexer_at_time('2021-10-30 03:10:50'))

Output

This will produce the following code −

DateTimeIndex...
DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-10-30 02:50:50+10:30',
'2021-10-30 03:10:50+10:30', '2021-10-30 03:30:50+10:30',
'2021-10-30 03:50:50+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='20T')
DateTimeIndex frequency...
<20 * Minutes>

Index locations of values at particular time of day...
[2]
Updated on: 2021-10-18T12:58:04+05:30

567 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements