Pandas | bdate_range method
Start your free 7-days trial now!
Pandas bdate_range(~) method returns a DatetimeIndex containing the specified business days.
Parameters
1. startlink | numeric or datetime-like | optional
The lower bound of the range. By default, start=None.
2. endlink | numeric or datetime-like | optional
The upper bound of the range. By default, end=None.
3. periodslink | int | optional
The desired number of dates. By default, periods=None.
Out of the above three parameters, you must only specify two.
4. freqlink | string or DateOffset | optional
The offset between two consecutive dates. By default, freq="B" (a single business day).
5. tzlink | str or None | optional
The timezone in which to encode the resulting DatetimeIndex.
6. normalizelink | boolean | optional
Whether or not to set the time unit to midnight for each date. By default, normalize=True.
7. namelink | string | optional
The name assigned to the resulting DatetimeIndex. By default, name=None.
8. weekmasklink | string or None | optional
The days of the week that are considered to be business days. By default, weekmask=None, which is equivalent to:
"Mon Tue Wed Thu Fri"
If you set weekmask, then you must also set freq="C", which stands for CustomBusinessDay.
9. holidayslink | array-like or None | optional
The days that are considered to be non-business days If you set holidays, then you must also set freq="C", which stands for CustomBusinessDay.
10. closed | string | optional
Whether or not to make the bounds inclusive/exclusive:
Value | Description |
|---|---|
|
|
|
|
| Both endpoints are inclusive. |
| Both endpoints are exclusive. |
By default, closed="right".
Return Value
A DatetimeIndex that holds the specified business days.
Examples
Basic usage
To get a DatetimeIndex that contains the business days between two dates:
pd.bdate_range(start='12/19/2019', end='12/24/2019')
DatetimeIndex(['2019-12-19', '2019-12-20', '2019-12-23', '2019-12-24'], dtype='datetime64[ns]', freq='B')
Notice 2019-12-21 (Saturday) and 2019-12-22 (Sunday) are missing since weekends are not considered to be business days by default.
Specifying periods
To get a DatetimeIndex of 3 business days:
pd.bdate_range(start='12/19/2019', periods=3)
DatetimeIndex(['2019-12-19', '2019-12-20', '2019-12-23'], dtype='datetime64[ns]', freq='B')
You could also specify a combination of end and periods like so:
pd.bdate_range(end='12/19/2019', periods=3)
DatetimeIndex(['2019-12-17', '2019-12-18', '2019-12-19'], dtype='datetime64[ns]', freq='B')
Specifying frequency
By default, freq="B", which means that an offset of a single business day will be used:
pd.bdate_range(start='12/19/2019', periods=3) # freq="B"
DatetimeIndex(['2019-12-19', '2019-12-20', '2019-12-23'], dtype='datetime64[ns]', freq='B')
To set an offset of 2 business days for each pair of consecutive dates:
pd.bdate_range(start='12/19/2019', periods=3, freq="2B")
DatetimeIndex(['2019-12-19', '2019-12-23', '2019-12-25'], dtype='datetime64[ns]', freq='2B')
Here, 2012-12-21 is a Saturday so this was excluded from the result.
Specifying tz
To encode the timezone of Asia/Tokyo into the resulting DatetimeIndex:
pd.bdate_range(start='12/20/2019', periods=3, tz="Asia/Tokyo")
DatetimeIndex(['2019-12-20 00:00:00+09:00', '2019-12-23 00:00:00+09:00', '2019-12-24 00:00:00+09:00'], dtype='datetime64[ns, Asia/Tokyo]', freq='B')
Specifying normalize
By default, normalize=True, which means the time unit of the dates are reset to midnight:
pd.bdate_range(start='12/20/2019 15:30:00', periods=3) # normalize=True
DatetimeIndex(['2019-12-20', '2019-12-23', '2019-12-24'], dtype='datetime64[ns]', freq='B')
To retain the information about the time unit, set normalize=False:
pd.bdate_range(start='12/20/2019 15:30:00', periods=3, normalize=False)
DatetimeIndex(['2019-12-20 15:30:00', '2019-12-23 15:30:00', '2019-12-24 15:30:00'], dtype='datetime64[ns]', freq='B')
Specifying name
To assign a name to the resulting DatetimeIndex:
idx = pd.bdate_range(start='12/20/2019', periods=2, name="My Dates")idx
DatetimeIndex(['2019-12-20', '2019-12-23'], dtype='datetime64[ns]', name='My Dates', freq='B')
Creating a DataFrame from this index:
pd.DataFrame({"A":[1,2]}, index=idx)
AMy Dates 2019-12-20 12019-12-23 2
Notice how the DataFrame's index shows the name we assigned.
Specifying weekmask
To change the days of the week that are considered to be business days, set the weekmask parameter like so:
pd.bdate_range(start='12/19/2019', freq="C", periods=4, weekmask="Mon Tue Wed Thu")
DatetimeIndex(['2019-12-19', '2019-12-23', '2019-12-24', '2019-12-25'], dtype='datetime64[ns]', freq='C')
When passing the weekmask parameter, you must also specify freq="C". Here, the C stands for CustomBusinessDay.
Specifying holidays
To set dates that are considered to be non-business days, set the holidays parameter like so:
holidays = ['2019-12-23']pd.bdate_range(start='12/19/2019', freq="C", periods=4, holidays=holidays)
DatetimeIndex(['2019-12-19', '2019-12-20', '2019-12-24', '2019-12-25'], dtype='datetime64[ns]', freq='C')
Notice how 2019-12-23, which is a Monday, is excluded from the returned DatetimeIndex.