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: Find closest date – without set_index – multiple conditions

We have the following Pandas Dataframe:

# Stackoverflow question
data = {'category':[1, 2, 3, 1, 2, 3, 1, 2, 3], 'date':['2000-01-01', '2000-01-01', '2000-01-01', '2000-01-02', '2000-01-02', '2000-01-02', '2000-01-03', '2000-01-03', '2000-01-03']}  
df = pd.DataFrame(data=data)
df['date'] = pd.to_datetime(df['date'])
df

category    date
0   1   2000-01-01
1   2   2000-01-01
2   3   2000-01-01
3   1   2000-01-02
4   2   2000-01-02
5   3   2000-01-02
6   1   2000-01-03
7   2   2000-01-03
8   3   2000-01-03

How can we query this dataframe to find the date 2000-01-02 with category 3? So we are looking for the row with index 5.

It should be accomplished without set_index(‘date’).
The reason for this is as follows, when setting the index on the actual data rather than the example data I receive the following error:

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

InvalidIndexError: Reindexing only valid with uniquely valued Index objects

>Solution :

Take a subset of relevant category, subtract the target date, and get idxmin

tmp = df.loc[df.category.eq(3)]
(tmp.date - pd.to_datetime("2000-01-02")).abs().idxmin()
# 5
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