How can you get the most recent business day in Python?

Viewed 4048

How can you get the most recent business day in python?

E.g., if today is a business day, I'd like to get today as a datetime object, but if today is a Sunday, I'd like to get Friday as a datetime object, presuming Friday is the most recent business day.

Thanks,

Jack

1 Answers

You can use:

import pandas as pd

pd.datetime.today() - pd.tseries.offsets.BDay(0)

Update

today = pd.datetime(2018,9,2)

np.where((today - pd.tseries.offsets.BDay(0)) > today,
         (today - pd.tseries.offsets.BDay(1)),
         (today - pd.tseries.offsets.BDay(0)))

[output]
Timestamp('2018-08-31 00:00:00')
Related