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 - Return index locations of values between particular time of day in DateTimeIndex
To return index locations of values between particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_between_time() method. This method is useful for filtering time-series data based on specific time ranges.
Syntax
DateTimeIndex.indexer_between_time(start_time, end_time, include_start=True, include_end=True)
Parameters
The method accepts the following parameters:
- start_time ? The start time in 'HH:MM:SS' format
- end_time ? The end time in 'HH:MM:SS' format
- include_start ? Boolean to include start time (default: True)
- include_end ? Boolean to include end time (default: True)
Creating a DateTimeIndex
First, let's create a DateTimeIndex with timezone information ?
import pandas as pd
# Create DatetimeIndex with period 5 and frequency as 20 minutes
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
print("DateTimeIndex...")
print(datetimeindex)
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')
Finding Index Locations Between Time Range
Now let's find index locations of values between specific times using indexer_between_time() ?
import pandas as pd
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
# Find index locations between 02:30:50 and 03:20:50
indices = datetimeindex.indexer_between_time('02:30:50', '03:20:50')
print("Index locations between 02:30:50 and 03:20:50:")
print(indices)
# Show the actual datetime values at these indices
print("\nCorresponding datetime values:")
print(datetimeindex[indices])
Index locations between 02:30:50 and 03:20:50:
[0 1 2]
Corresponding datetime values:
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'],
dtype='datetime64[ns, Australia/Adelaide]', freq='20T')
Complete Example
Here's a comprehensive example showing different time filtering operations ?
import pandas as pd
# Create DatetimeIndex
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
print("DateTimeIndex...")
print(datetimeindex)
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Find index at specific time
print("\nIndex locations at time 03:10:50...")
specific_time_indices = datetimeindex.indexer_at_time('03:10:50')
print(specific_time_indices)
# Find indices between time range
print("\nIndex locations between 02:30:50 and 03:20:50...")
range_indices = datetimeindex.indexer_between_time('02:30:50', '03:20:50')
print(range_indices)
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 at time 03:10:50...
[2]
Index locations between 02:30:50 and 03:20:50...
[0 1 2]
Conclusion
The indexer_between_time() method efficiently returns integer indices for datetime values falling within a specified time range. This is particularly useful for time-series data analysis and filtering operations on DateTimeIndex objects.
