summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/datetime_busday.c32
-rw-r--r--numpy/core/src/multiarray/datetime_busdaydef.c45
-rw-r--r--numpy/core/src/multiarray/datetime_busdaydef.h1
-rw-r--r--numpy/core/tests/test_datetime.py3
4 files changed, 56 insertions, 25 deletions
diff --git a/numpy/core/src/multiarray/datetime_busday.c b/numpy/core/src/multiarray/datetime_busday.c
index ce38a035d..21647d1ea 100644
--- a/numpy/core/src/multiarray/datetime_busday.c
+++ b/numpy/core/src/multiarray/datetime_busday.c
@@ -475,6 +475,15 @@ finish:
return ret;
}
+NPY_NO_EXPORT npy_intp
+business_day_count(PyArrayObject *dates_begin, PyArrayObject *dates_end,
+ PyArrayObject *out,
+ NPY_BUSDAY_ROLL roll,
+ npy_bool *weekmask, int busdays_in_weekmask,
+ npy_datetime *holidays_begin, npy_datetime *holidays_end)
+{
+}
+
static int
PyArray_BusDayRollConverter(PyObject *roll_in, NPY_BUSDAY_ROLL *roll)
{
@@ -925,9 +934,10 @@ array_busday_offset(PyObject *NPY_UNUSED(self),
/* Indicate that the holidays weren't allocated by us */
allocated_holidays = 0;
- /* Copy the weekmask/holidays data */
- memcpy(weekmask, busdaydef->weekmask, 7);
+ /* Copy the private normalized weekmask/holidays data */
holidays = busdaydef->holidays;
+ busdays_in_weekmask = busdaydef->busdays_in_weekmask;
+ memcpy(weekmask, busdaydef->weekmask, 7);
}
else {
/*
@@ -937,6 +947,15 @@ array_busday_offset(PyObject *NPY_UNUSED(self),
if (weekmask[0] == 2) {
weekmask[0] = 1;
}
+
+ /* Count the number of business days in a week */
+ busdays_in_weekmask = 0;
+ for (i = 0; i < 7; ++i) {
+ busdays_in_weekmask += weekmask[i];
+ }
+
+ /* The holidays list must be normalized before using it */
+ normalize_holidays_list(&holidays, weekmask);
}
/* Make 'dates' into an array */
@@ -979,15 +998,6 @@ array_busday_offset(PyObject *NPY_UNUSED(self),
out = (PyArrayObject *)out_in;
}
- /* Count the number of business days in a week */
- busdays_in_weekmask = 0;
- for (i = 0; i < 7; ++i) {
- busdays_in_weekmask += weekmask[i];
- }
-
- /* The holidays list must be normalized before using it */
- normalize_holidays_list(&holidays, weekmask);
-
ret = business_day_offset(dates, offsets, out, roll,
weekmask, busdays_in_weekmask,
holidays.begin, holidays.end);
diff --git a/numpy/core/src/multiarray/datetime_busdaydef.c b/numpy/core/src/multiarray/datetime_busdaydef.c
index d27542ea5..74b3eeef7 100644
--- a/numpy/core/src/multiarray/datetime_busdaydef.c
+++ b/numpy/core/src/multiarray/datetime_busdaydef.c
@@ -31,7 +31,12 @@ busdaydef_new(PyTypeObject *subtype,
self = (PyArray_BusinessDayDef *)subtype->tp_alloc(subtype, 0);
if (self != NULL) {
- /* Set the weekmask to the default */
+ /* Start with an empty holidays list */
+ self->holidays.begin = NULL;
+ self->holidays.end = NULL;
+
+ /* Set the weekmask to the default */
+ self->busdays_in_weekmask = 5;
self->weekmask[0] = 1;
self->weekmask[1] = 1;
self->weekmask[2] = 1;
@@ -39,10 +44,6 @@ busdaydef_new(PyTypeObject *subtype,
self->weekmask[4] = 1;
self->weekmask[5] = 0;
self->weekmask[6] = 0;
-
- /* Start with an empty holidays list */
- self->holidays.begin = NULL;
- self->holidays.end = NULL;
}
return (PyObject *)self;
@@ -52,8 +53,17 @@ static int
busdaydef_init(PyArray_BusinessDayDef *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"weekmask", "holidays", NULL};
+ int i, busdays_in_weekmask;
+
+ /* Clear the holidays if necessary */
+ if (self->holidays.begin != NULL) {
+ PyArray_free(self->holidays.begin);
+ self->holidays.begin = NULL;
+ self->holidays.end = NULL;
+ }
/* Reset the weekmask to the default */
+ self->busdays_in_weekmask = 5;
self->weekmask[0] = 1;
self->weekmask[1] = 1;
self->weekmask[2] = 1;
@@ -62,24 +72,31 @@ busdaydef_init(PyArray_BusinessDayDef *self, PyObject *args, PyObject *kwds)
self->weekmask[5] = 0;
self->weekmask[6] = 0;
- /* Clear the holidays if necessary */
- if (self->holidays.begin != NULL) {
- PyArray_free(self->holidays.begin);
- self->holidays.begin = NULL;
- self->holidays.end = NULL;
- }
-
/* Parse the parameters */
if (!PyArg_ParseTupleAndKeywords(args, kwds,
- "|O&O&", kwlist,
+ "|O&O&:busdaydef", kwlist,
&PyArray_WeekMaskConverter, &self->weekmask[0],
&PyArray_HolidaysConverter, &self->holidays)) {
return -1;
}
-
+
+ /* Count the number of business days in a week */
+ busdays_in_weekmask = 0;
+ for (i = 0; i < 7; ++i) {
+ busdays_in_weekmask += self->weekmask[i];
+ }
+ self->busdays_in_weekmask = busdays_in_weekmask;
+
/* Normalize the holidays list */
normalize_holidays_list(&self->holidays, self->weekmask);
+ if (self->busdays_in_weekmask == 0) {
+ PyErr_SetString(PyExc_ValueError,
+ "Cannot construct a numpy.busdaydef with a weekmask of "
+ "all zeros");
+ return -1;
+ }
+
return 0;
}
diff --git a/numpy/core/src/multiarray/datetime_busdaydef.h b/numpy/core/src/multiarray/datetime_busdaydef.h
index 262143e70..dafc5c07d 100644
--- a/numpy/core/src/multiarray/datetime_busdaydef.h
+++ b/numpy/core/src/multiarray/datetime_busdaydef.h
@@ -4,6 +4,7 @@
typedef struct {
PyObject_HEAD
npy_holidayslist holidays;
+ int busdays_in_weekmask;
npy_bool weekmask[7];
} PyArray_BusinessDayDef;
diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py
index a45994691..ecac60e5d 100644
--- a/numpy/core/tests/test_datetime.py
+++ b/numpy/core/tests/test_datetime.py
@@ -1217,6 +1217,9 @@ class TestDateTime(TestCase):
# Default M-F weekmask
assert_equal(bdd.weekmask, np.array([1,1,1,1,1,0,0], dtype='?'))
+ # All-zeros weekmask should raise
+ assert_raises(ValueError, np.busdaydef, weekmask=[0,0,0,0,0,0,0])
+
def test_datetime_busday_holidays_offset(self):
# With exactly one holiday
assert_equal(