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 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. This method returns an array of integer positions where the time component matches the specified time.
Syntax
DateTimeIndex.indexer_at_time(time, asof=False)
Parameters
The key parameters are ?
- time ? Time as a time object or string
- asof ? Return the latest index location if exact time not found (default: False)
Creating DateTimeIndex
First, let's create a DateTimeIndex with timezone-aware timestamps ?
import pandas as pd
# Create DatetimeIndex with 20-minute intervals
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='20T')
print("DateTimeIndex...")
print(datetimeindex)
print("\nFrequency:", datetimeindex.freq)
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')
Frequency: <20 * Minutes>
Finding Index Locations by Time
Use indexer_at_time() to get the integer positions of timestamps matching a specific 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 for specific time
locations = datetimeindex.indexer_at_time('2021-10-30 03:10:50')
print("Index locations at 03:10:50:", locations)
# You can also use just the time part
locations_time_only = datetimeindex.indexer_at_time('03:10:50')
print("Using time only:", locations_time_only)
Index locations at 03:10:50: [2] Using time only: [2]
Multiple Matches Example
When multiple timestamps have the same time component, all matching positions are returned ?
import pandas as pd
# Create index spanning multiple days with same times
dates = ['2021-10-30 15:30:00', '2021-10-31 15:30:00', '2021-11-01 15:30:00', '2021-11-02 10:00:00']
multi_day_index = pd.DatetimeIndex(dates)
print("Multi-day DateTimeIndex:")
print(multi_day_index)
# Find all positions with time 15:30:00
locations = multi_day_index.indexer_at_time('15:30:00')
print("\nPositions with time 15:30:00:", locations)
Multi-day DateTimeIndex:
DatetimeIndex(['2021-10-30 15:30:00', '2021-10-31 15:30:00',
'2021-11-01 15:30:00', '2021-11-02 10:00:00'],
dtype='datetime64[ns]', freq=None)
Positions with time 15:30:00: [0 1 2]
Comparison with Related Methods
| Method | Returns | Use Case |
|---|---|---|
indexer_at_time() |
Integer positions | Get index locations for time-based selection |
at_time() |
DateTimeIndex subset | Filter DateTimeIndex by time |
between_time() |
DateTimeIndex subset | Filter by time range |
Conclusion
The indexer_at_time() method efficiently returns integer positions of timestamps matching a specific time component. Use this method when you need index positions rather than the actual datetime values for further operations.
