How can I efficiently implement a vectorized version of the "Modified Following" business day payment rule in Pandas?
Background
In finance it is often the case that fixed income coupons are paid on a "modified following" basis, that is, if the coupon date falls on a weekend or holiday, it is paid on the following business day, unless said following business day falls into the next calendar month, in which case we go backwards to the nearest previous business day.
April 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
April 2017 offers a good example. If a payment falls on say, Saturday 15th, it will be moved forward to Monday 17th. However if it falls on Saturday 29th, two weeks later, it will be moved backwards to Friday 28th (since the following Monday will be in May).
I need to do this for thousands of dates at a time, hence I'd like it to be vectorised and pandas efficient if possible (that is, I'd like to avoid just running each date through an if-then-else). So given a vector of dates, it would check which fall on Sat/Sun and automatically apply the rule, outputting a vector of "modified following" dates.