I have a dataframe having the following columns
'CUSTOMER_ID','ORDERED_TIME','ORDER_ID'
Here, ORDERED_TIME is a timestamp field indicating when the order was placed, CUSTOMER_ID is the customer id (INTEGER) and ORDER_ID is the order_id (INTEGER)
Now, from this dataframe, for every order made in the month of Jan to March 2021, I need to find out the time the previous order was placed just before ORDERED_TIME. Let's call this column PREV_ORDER_TIME
The dataframe includes order data all the way back to 2019. However, I am only computing PREV_ORDER_TIME for all orders made between Jan to March. If there is no PREV_ORDER_TIME order for an order, I would like to display null
My final expected output is something like ['CUSTOMER_ID','ORDERED_TIME','ORDER_ID','PREV_ORDER_TIME']
The way I understand, for some orders in these 3 months, the previous orders can be in these 3 months, but for some other orders, it may well be before this window.
I am not sure how to approach this. I feel like I might need to use some binary search to find the closest date, but am not really sure how to make this work only for orders from Jan to March despite my dataset containing orders well before that.