I have a datetime instance with dates (dfDates):
2017-03-01 00:00
2017-03-02 00:00
2017-03-04 00:00
...
For the last day (here: 2017-03-04) I calculate the previous day/month/year/etc. as follows:
def previous_day(dtToday):
return dtToday - pd.DateOffset(days=1)
This returns 2017-03-03. However, this business day is not available in my range of dates (dfDates).
I am therefore looking for a robust way to find the date that is the closest to the previous day/month/year/etc.. In this case for the previous day it should return 2017-03-02.
Note, I understand that you can do something like index -1 to get the previous day. It becomes however complicated when taking the previous month (there are not always 30 days in a month) and even the previous year (there are not always 252 working days in a year). Is there therefore a robust method to get the closest available date?
Update
I understand also that you can use timedelta as follows:
from datetime import datetime, timedelta
d = datetime.today() - timedelta(days=days_to_subtract)
However, how does that relate to dtToday and how can I link it with dfDates? dtToday in my case is not always datetime.today(). Sometimes its a random date.
d = datetime.today() - timedelta(days=days_to_subtract)but where in that is my dtToday and how does it link todfDates. I am using a specific datedtTodaywhich is not alwaysdatetime.today()- JohnAndrews