1

Does anyone know how to add x business months to a datetime object with pandas? I know you can do this for business days as, e.g.:

datetime.datetime(2017, 11, 23) + pd.tseries.offsets.BDay(20)

Or you can add a month as:

datetime.datetime(2017, 11, 23) + pd.tseries.offsets.DateOffset(months=1)

But this is not giving the correct date for a business month but rather a calendar month. My definition of a business month is to exclude weekends and US Holidays for example.

Any ideas? Thanks!

3
  • 2
    What is your definition of a business month? i.e. what result do you want from your example?
    – Ian Ash
    Nov 23, 2017 at 11:20
  • I would like to exclude weekends and US Holidays for example
    – tsando
    Nov 23, 2017 at 11:48
  • Not sure you can exclude holidays. Most likely you would have to do it by converting the business months into business days.
    – Roars
    Nov 23, 2017 at 11:51

2 Answers 2

1

Actually I just realised a potential solution for 1 business month (defined as excluding weekends and US holidays) could be:

pd.tseries.offsets.BMonthEnd().rollforward(date) + pd.tseries.offsets.BDay(1)
1

When the business day convention is Following:

  1. Add n solar months, as in your example
  2. Add 0 business days. This will adjust the date to match the first following business day, only in case the result of the first calculation doesn't fall on a business day.

The code above modified accordingly:

from datetime import datetime
from pandas.tseries.offsets import DateOffset, BDay

datetime(2017, 11, 23) + DateOffset(months=1) + BDay(0)

This seems to work for me. I am using pandas 0.25.1 on python 3.7.4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.