From dac39845483a44a281a4926ec71a0ee46e83c98f Mon Sep 17 00:00:00 2001 From: Alok Singhal Date: Sat, 16 Jul 2011 11:08:41 -0500 Subject: [PATCH 1/2] make np.busday_count return negative values when the start date is after end date --- numpy/core/src/multiarray/datetime_busday.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/numpy/core/src/multiarray/datetime_busday.c b/numpy/core/src/multiarray/datetime_busday.c index 7ad033248585..c16848e59467 100644 --- a/numpy/core/src/multiarray/datetime_busday.c +++ b/numpy/core/src/multiarray/datetime_busday.c @@ -365,6 +365,7 @@ apply_business_day_count(npy_datetime date_begin, npy_datetime date_end, { npy_int64 count, whole_weeks; int day_of_week = 0; + int swapped = 0; /* If we get a NaT, raise an error */ if (date_begin == NPY_DATETIME_NAT || date_end == NPY_DATETIME_NAT) { @@ -375,9 +376,14 @@ apply_business_day_count(npy_datetime date_begin, npy_datetime date_end, } /* Trivial empty date range */ - if (date_begin >= date_end) { + if (date_begin == date_end) { *out = 0; return 0; + } else if (date_begin > date_end) { + npy_datetime tmp = date_begin; + date_begin = date_end; + date_end = tmp; + swapped = 1; } /* Remove any earlier holidays */ @@ -411,6 +417,10 @@ apply_business_day_count(npy_datetime date_begin, npy_datetime date_end, } } + if (swapped) { + count = -count; + } + *out = count; return 0; } @@ -563,6 +573,9 @@ business_day_offset(PyArrayObject *dates, PyArrayObject *offsets, * the end date. This is the low-level function which requires already * cleaned input data. * + * If dates_begin is before dates_end, the result is positive. If + * dates_begin is after dates_end, it is negative. + * * dates_begin: An array of dates with 'datetime64[D]' data type. * dates_end: An array of dates with 'datetime64[D]' data type. * out: Either NULL, or an array with 'int64' data type From 2fb06dbcb9aa2ca8d23c7a6e4e6d8c6e6315cf64 Mon Sep 17 00:00:00 2001 From: Alok Singhal Date: Tue, 19 Jul 2011 12:15:13 -0700 Subject: [PATCH 2/2] coding style --- numpy/core/src/multiarray/datetime_busday.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/numpy/core/src/multiarray/datetime_busday.c b/numpy/core/src/multiarray/datetime_busday.c index c16848e59467..5a0078b76d04 100644 --- a/numpy/core/src/multiarray/datetime_busday.c +++ b/numpy/core/src/multiarray/datetime_busday.c @@ -379,7 +379,8 @@ apply_business_day_count(npy_datetime date_begin, npy_datetime date_end, if (date_begin == date_end) { *out = 0; return 0; - } else if (date_begin > date_end) { + } + else if (date_begin > date_end) { npy_datetime tmp = date_begin; date_begin = date_end; date_end = tmp;