Pandas 1.5.3 Index.get_indexer not working like index.get_loc

Question:

I recently updated Pandas to the latest version, 1.5.3. I was using a version pre 1.4 previously. With the newest update I am getting a bunch of depreciation notices on index.get_loc and I am to use index.get_indexer. I updated my code to get_indexer, but now I am receiving a bunch of errors.

My code uses timestamps as the index, and with get_loc I could simply find the index number by passing in a datetime. Now I get an error: ‘datetime.datetime’ object is not iterable.

I have some simple sample code below:

from datetime import datetime, timedelta

import pandas as pd
import numpy as np

expiration_date = datetime(2016,3,24,16,00,0)
num_minutes = 50000

date_list = [expiration_date - timedelta(minutes=x) for x in range(num_minutes)]

df = pd.DataFrame(index = date_list)

searchDate = date_list[10]

idx_get_loc = df.index.get_loc(searchDate, method='nearest')
print(f'Closes index using get_loc: {idx_get_loc}')

idx_get_indexer = df.index.get_indexer(searchDate, method='nearest')  
print(f'Closes index using get_indexer: {idx_get_indexer}')

get_loc will report out the proper index location. get_indexer fails. What am I missing?

Asked By: Craig Evans

||

Answers:

Here, the get_indexer() method expects an iterable object such as a list or an array of timestamps, not a single timestamp as an argument. To use the get_indexer() method with a single timestamp, you need to put the timestamp in a list or an array like below.

idx_get_indexer = df.index.get_indexer([searchDate], method='nearest')
Answered By: Sharuq