Joe Root
Joe Root

Reputation: 13

Selecting only rows where one date is less than another with Pandas Dataframe

I have a pandas dataframe with this data:

Id                  Converteddate       Createddate
0015000000toohpAAA  2015-07-24 00:00:00 2014-07-08 19:36:13
0015000000tqEpKAAU  2015-03-17 00:00:00 2014-07-16 00:28:06
00138000015me01AAA  2015-10-22 00:00:00 2015-10-22 22:04:55
00138000015me56AAA  2015-10-22 00:00:00 2015-10-22 22:17:52

I'm trying to only keep rows where ConvertedDate <= CreatedDate + 3 days, so I converted both strings to datetime, then used timedelta to calculate the 3 days. I don't get any error codes, but my output dataframe is keeping records that don't meet my criteria of ConvertedDate <= CreatedDate + 3 days.

netnewframe['CreatedDate'] = netnewframe.apply(lambda row: ToDateTimeObj(row['CreatedDate']), axis=1)
netnewframe['ConvertedDate'] = netnewframe.apply(lambda row: ToDateTimeObj(row['ConvertedDate']), axis=1)
netnewframe = netnewframe[(netnewframe.ConvertedDate <= (netnewframe.CreatedDate + timedelta(days=3)))]

Upvotes: 1

Views: 1997

Answers (1)

jezrael
jezrael

Reputation: 862441

You can use for converting to datetime function to_datetime and then use to_timedelta:

netnewframe['CreatedDate'] = pd.to_datetime(netnewframe['CreatedDate'])
netnewframe['ConvertedDate'] = pd.to_datetime(netnewframe['ConvertedDate'])
netnewframe = netnewframe[netnewframe.ConvertedDate <= (netnewframe.CreatedDate + 
                                                        pd.to_timedelta(3, unit='d'))]
print (netnewframe)
                   Id ConvertedDate         CreatedDate
2  00138000015me01AAA    2015-10-22 2015-10-22 22:04:55
3  00138000015me56AAA    2015-10-22 2015-10-22 22:17:52

Upvotes: 1

Related Questions