Skip to content

Businessday make np.busday_count return negative values when the start date is after end date #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion numpy/core/src/multiarray/datetime_busday.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -375,10 +376,16 @@ 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 */
holidays_begin = find_earliest_holiday_on_or_after(date_begin,
Expand Down Expand Up @@ -411,6 +418,10 @@ apply_business_day_count(npy_datetime date_begin, npy_datetime date_end,
}
}

if (swapped) {
count = -count;
}

*out = count;
return 0;
}
Expand Down Expand Up @@ -563,6 +574,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
Expand Down