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

Select Rows Based on Time Difference [Before or After] In Columns

I have the following dataset of students taking 2 different exams:

df = pd.DataFrame({'student': 'A B C D E'.split(),
                  'sat_date': [datetime.datetime(2013,4,1),datetime.datetime(2013,5,1),
                               datetime.datetime(2013,5,2),datetime.datetime(2013,7,15),
                               datetime.datetime(2013,8,1)],
                   'act_date': [datetime.datetime(2013,4,12),datetime.datetime(2013,5,2),
                               datetime.datetime(2013,4,12), datetime.datetime(2013,7,1),
                               datetime.datetime(2013,8,2)]})

print(df)

student   sat_date   act_date
0       A 2013-04-01 2013-04-12
1       B 2013-05-01 2013-05-02
2       C 2013-05-02 2013-04-12
3       D 2013-07-15 2013-07-01
4       E 2013-08-01 2013-08-02


I want to select those students whose two exams are 10 days apart from each other in either direction.

I am trying Timedelta, but I’m not sure if it’s optimal.

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

df[(df['sat_date'] >= df['act_date'] + pd.Timedelta(days=10)) | (df['sat_date'] <= df['act_date'] - pd.Timedelta(days=10))]

Desired Output:

student sat_date    act_date
0   A   2013-04-01  2013-04-12
2   C   2013-05-02  2013-04-12
3   D   2013-07-15  2013-07-01

Is there any better way of getting the desired output? Any suggestions would be appreciated. Thanks!

>Solution :

I would probably look at the absolute value of the difference between the two dates is greater to or equal than 10.

df.loc[abs((df['sat_date']-df['act_date']).dt.days).ge(10)]
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