Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I have 2 datetime columns like this:

Column 1:

0   2016-06-30 16:57:12.219297

1   2016-07-19 16:02:21.408984

2   2016-07-19 16:02:21.408984

3   2017-08-31 16:35:14.414915

4   2017-08-31 16:35:14.414915

5   2016-07-14 19:39:21.029636

6   2018-07-30 15:10:41.919970

7   2016-08-16 13:50:16.941665

8   2017-04-27 22:19:32.264015

9   2018-06-25 19:47:42.126527

Column 2:

0   2019-07-02 21:06:06.080788

1   2019-06-02 21:58:55.188987

2   2019-06-02 21:59:25.069153

3   2016-07-14 19:39:21.029636

4   2018-07-30 15:10:41.919970

5   2017-08-31 16:35:14.414915

6   2017-08-31 16:35:14.414915

7   2019-01-21 15:20:07.471152

8   2017-07-20 17:24:44.189102

9   2017-07-20 17:24:44.189102

I want to calculate the time difference between the 2 and get the results in days. Once this is done, i want to conserve those results between and interval of < 45 days and > 45 days.

How can i do this?

Thanks in advance

1 Answer

0 votes
by (25.1k points)

Use Series.dt.days for converting timedeltas to days and then filter by boolean indexing:

df['diff'] = (df['col2'] - df['col1']).dt.days

df1 = df[df['diff'] < 45]

df2 = df[df['diff'] > 45]

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...