Skip to content
Advertisement

Most recent previous business day in Python

I need to subtract business days from the current date.

I currently have some code which needs always to be running on the most recent business day. So that may be today if we’re Monday thru Friday, but if it’s Saturday or Sunday then I need to set it back to the Friday before the weekend. I currently have some pretty clunky code to do this:

JavaScript

Is there a better way?

Can I tell timedelta to work in weekdays rather than calendar days for example?

Advertisement

Answer

Use pandas!

JavaScript

Since today is Thursday, Sept 26, that will give you an output of:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement