sl624 sl624 - 1 month ago 8
Python Question

how to roll pandas datetime 5 minute into the previous business day

consider following pandas dataframe df:

datestimes,price
2016-10-28 23:55:00,100.0
2016-10-31 00:00:00,122.4


after roll the time back by 5 minute:

df.datestimes - pd.Timedelta(minutes=5)


output:

datestimes,price
2016-10-28 23:50:00,100.0
2016-10-30 23:55:00,122.4


2016-10-30 is not a business day and 2016-10-28 is, how do i move it to 2016-10-28 23:55:00 as the following?

datestimes,price
2016-10-28 23:50:00,100.0
2016-10-28 23:55:00,122.4

Answer

the typical answer is to go one business day in the past + 1 day in the future:

df.datestimes  +pd.datetools.Day(1) - pd.datetools.BDay(1)