Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas 1.5.3 Index.get_indexer not working like index.get_loc

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading