[Numpy-discussion] datetime business day API

Mark Wiebe mwwiebe at gmail.com
Thu Jun 9 18:23:46 EDT 2011


Here's a possible design for a business day API for numpy datetimes:


The 'B' business day unit will be removed. All business day-related
calculations will be done using the 'D' day unit.

A class *BusinessDayDef* to encapsulate the definition of the business week
and holidays. The business day functions will either take one of these
objects, or separate weekmask and holidays parameters, to specify the
business day definition. This class serves as both a performance
optimization and a way to encapsulate the weekmask and holidays together,
for example if you want to make a dictionary mapping exchange names to their
trading days definition.

The weekmask can be specified in a number of ways, and internally becomes a
boolean array with 7 elements with True for the days Monday through Sunday
which are valid business days. Some different notations are for the 5-day
week include [1,1,1,1,1,0,0], "1111100" "MonTueWedThuFri". The holidays are
always specified as a one-dimensional array of dtype 'M8[D]', and are
internally used in sorted form.


A function *is_busday*(datearray, weekmask=, holidays=, busdaydef=) returns
a boolean array matching the input datearray, with True for the valid
business days.

A function *busday_offset*(datearray, offsetarray, roll='raise', weekmask=,
holidays=, busdaydef=) which first applies the 'roll' policy to start at a
valid business date, then offsets the date by the number of business days
specified in offsetarray. The arrays datearray and offsetarray are broadcast
together. The 'roll' parameter can be 'forward'/'following',
'backward'/'preceding', 'modifiedfollowing', 'modifiedpreceding', or 'raise'
(the default).

A function *busday_count*(datearray1, datearray2, weekmask=, holidays=,
busdaydef=) which calculates the number of business days between datearray1
and datearray2, not including the day of datearray2.


For example, to find the first Monday in Feb 2011,

>>>np.busday_offset('2011-02', 0, roll='forward', weekmask='Mon')

or to find the number of weekdays in Feb 2011,

>>>np.busday_count('2011-02', '2011-03')

This set of three functions appears to be powerful enough to express the
business-day computations that I've been shown thus far.

Cheers,
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110609/e55000f9/attachment.html>


More information about the NumPy-Discussion mailing list