Skip to content Skip to sidebar Skip to footer

Pandas: Date Range Creation

I'd like to create a monthly date range within Pandas taking in consideration of business days. such as: dt_range = pd.date_range(start='2017-12-20', periods=5, freq='M') Meaning:

Solution 1:

One way to solve it:

dt_range = pd.date_range(start='2017-12-20', periods=5, freq='MS') + pd.DateOffset(days=18) + pd.tseries.offsets.BusinessDay(1)

Output:

DatetimeIndex(['2018-01-22', '2018-02-20', '2018-03-20', '2018-04-20',
               '2018-05-21'],
              dtype='datetime64[ns]', freq=None)

I just go to the first day of the month with the date_range(.., freq='MS) and then I add 18 days to get to 19th day. Then I use offsets.BusinessDay as described in this SO post to find the next business day.

If you want to include your start day, you have to start one month earlier. This behavior is somewhat peculiar, but easy to account for.

Post a Comment for "Pandas: Date Range Creation"