Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get business days between start and end date using pandas

Tags:

python

pandas

I'm using pandas and I'm wondering what's the easiest way to get the business days between a start and end date using pandas?

There are a lot of posts out there regarding doing this in Python (for example), but I would be interested to use directly pandas as I think that pandas can probably handle this quite easy.

like image 622
Thomas Kremmel Avatar asked Oct 22 '12 20:10

Thomas Kremmel


2 Answers

You can also use date_range for this purpose.

In [3]: pd.date_range('2011-01-05', '2011-01-09', freq=BDay())  Out[3]: DatetimeIndex(['2011-01-05', '2011-01-06', '2011-01-07'], dtype='datetime64[ns]', freq='B', tz=None) 

EDIT

Or even simpler

In [7]: pd.bdate_range('2011-01-05', '2011-01-09')  Out[7]: DatetimeIndex(['2011-01-05', '2011-01-06', '2011-01-07'], dtype='datetime64[ns]', freq='B', tz=None) 

Note that both start and end dates are inclusive. Source: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.bdate_range.html

like image 161
Romain Avatar answered Oct 06 '22 19:10

Romain


As of v0.14 you can use holiday calendars.

 from pandas.tseries.holiday import USFederalHolidayCalendar from pandas.tseries.offsets import CustomBusinessDay  us_bd = CustomBusinessDay(calendar=USFederalHolidayCalendar()) print pd.DatetimeIndex(start='2010-01-01',end='2010-01-15', freq=us_bd) 

returns:

 DatetimeIndex(['2010-01-04', '2010-01-05', '2010-01-06', '2010-01-07',                '2010-01-08', '2010-01-11', '2010-01-12', '2010-01-13',                '2010-01-14', '2010-01-15'],               dtype='datetime64[ns]', freq='C') 
like image 31
Ivelin Avatar answered Oct 06 '22 21:10

Ivelin