Python Pandas - Return index locations of values between particular time of day including start time in DateTimeIndex

To return index locations of values between particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_between_time() method. Set the include_start parameter to True for including the start time.

Syntax

DateTimeIndex.indexer_between_time(start_time, end_time, include_start=True, include_end=True)

Parameters

Parameter Description
start_time Start time as string in format 'HH:MM:SS'
end_time End time as string in format 'HH:MM:SS'
include_start Whether to include start time (default: True)
include_end Whether to include end time (default: True)

Example

Let's create a DatetimeIndex and find index locations between specific times ?

import pandas as pd

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

# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)

# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)

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

# Find index locations of values between particular time of day
# The "start_time" is set '03:10:50' and "end_time" '03:50:50'
print("\nIndex locations of values between particular time of day...")
print(datetimeindex.indexer_between_time('03:10:50','03:50:50', include_start=True))
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', '2021-10-30 04:10:50+10:30',
               '2021-10-30 04:30:50+10:30'],
              dtype='datetime64[ns, Australia/Adelaide]', freq='20T')

DateTimeIndex frequency...
<20 * Minutes>

Index locations of values at particular time of day...
[2]

Index locations of values between particular time of day...
[2 3 4]

Understanding the Results

The method returns index positions as an array. In our example:

  • Index 2 corresponds to '2021-10-30 03:10:50' (start time)
  • Index 3 corresponds to '2021-10-30 03:30:50' (middle time)
  • Index 4 corresponds to '2021-10-30 03:50:50' (end time)

Excluding Start Time

To exclude the start time, set include_start=False ?

import pandas as pd

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

# Exclude start time
print("Index locations excluding start time...")
print(datetimeindex.indexer_between_time('03:10:50','03:50:50', include_start=False))
Index locations excluding start time...
[3 4]

Conclusion

Use indexer_between_time() to get index positions of timestamps within a specific time range. The include_start parameter controls whether the start time boundary is included in the results.

Updated on: 2026-03-26T17:14:30+05:30

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements