search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas | bdate_range method

schedule Aug 10, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
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.

WARNING

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

"left"

  • Left endpoint is inclusive.

  • Right endpoint is exclusive.

"right"

  • Left endpoint is exclusive.

  • Right endpoint is inclusive.

"both"

Both endpoints are inclusive.

"neither"

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)
A
My Dates
2019-12-20 1
2019-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.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!