Skip to content

BUG: Fix busday_count for reversed dates#23229

Merged
seberg merged 5 commits intonumpy:mainfrom
eendebakpt:busday
Mar 20, 2023
Merged

BUG: Fix busday_count for reversed dates#23229
seberg merged 5 commits intonumpy:mainfrom
eendebakpt:busday

Conversation

@eendebakpt
Copy link
Copy Markdown
Contributor

Fixes #23197

This is a backwards incompatible change, so we need to add a release note.

@seberg seberg added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Mar 9, 2023
@seberg
Copy link
Copy Markdown
Member

seberg commented Mar 9, 2023

Just to copy it here, we discussed the issue in the triage meeting and think we should just do it, as it seems clearly broken right now. This definitely needs the release note, though. I am also wondering if the test covers which day is excluded (start or end) in the path where only one of them is a business day?

@eendebakpt eendebakpt changed the title Draft: BUG: Fix busday_count for reversed dates BUG: Fix busday_count for reversed dates Mar 13, 2023
@eendebakpt eendebakpt requested review from eltoder and seberg and removed request for eltoder and seberg March 13, 2023 08:13
@seberg
Copy link
Copy Markdown
Member

seberg commented Mar 13, 2023

Hmmm, somehow I had misremembered it as just including the enddate, but it actually does exclude the startdate for negative values (i.e. it always excludes the larger date). I am OK with giving it a shot, but it does make me a bit less confident that nobody might be working around this in weird ways.

I guess we merge it ping the mailing list?

@eendebakpt
Copy link
Copy Markdown
Contributor Author

Hmmm, somehow I had misremembered it as just including the enddate, but it actually does exclude the startdate for negative values (i.e. it always excludes the larger date). I am OK with giving it a shot, but it does make me a bit less confident that nobody might be working around this in weird ways.

I guess we merge it ping the mailing list?

Sounds good. You want me to send an email to the mailing list?

@seberg
Copy link
Copy Markdown
Member

seberg commented Mar 13, 2023

Happy if you do :).

@charris
Copy link
Copy Markdown
Member

charris commented Mar 13, 2023

@Kai-Striega The suggestion has been made to move busday functionality to numpy-financial at some point. What are your thoughts about that suggestion? It might be a bit messy, as the code is in C and depends on numpy.datetime.

@stefanv
Copy link
Copy Markdown
Contributor

stefanv commented Mar 14, 2023

Counts the number of valid days between begindates and
enddates, not including the day of enddates.

If enddates specifies a date value that is earlier than the
corresponding begindates date value, the count will be negative.

The function calculates the delta for a half open bracket. Question is: what is that bracket when you have enddates < begindates.

For indexing, we always exclude the last:

In [13]: x[1:4]
Out[13]: array([1, 2, 3])

In [15]: x[4:1:-1]
Out[15]: array([4, 3, 2])

Current behavior:

In [16]: np.busday_count(np.datetime64('2023-03-12'), np.datetime64('2023-03-13'))
Out[16]: 0

Sunday to Monday, with Monday excluded. Makes sense. But:

In [17]: np.busday_count(np.datetime64('2023-03-13'), np.datetime64('2023-03-12'))
Out[17]: 0

Monday through Sunday, would have expected -1.

The current behavior (a) does not match the docstring and (b) is hard to predict, so would support fixing it: always exclude the enddates.

@Kai-Striega
Copy link
Copy Markdown
Member

Kai-Striega commented Mar 14, 2023

@Kai-Striega The suggestion has been made to move busday functionality to numpy-financial at some point. What are your thoughts about that suggestion? It might be a bit messy, as the code is in C and depends on numpy.datetime.

A few thoughts:

  • I believe the functionality would fit in with numpy-financial's scope
  • They could be useful for someone – but I can't think of a use case of the top of my head
  • I don't have much experience working with / maintaining C, however, this seems like a good time to learn
  • This would require a new release of numpy-financial. I've been planning this for a while anyway, it might be a good time to do one.

@eendebakpt
Copy link
Copy Markdown
Contributor Author

@seberg
Copy link
Copy Markdown
Member

seberg commented Mar 20, 2023

Let's give this a shot then, thanks.

@seberg seberg merged commit 1cf8822 into numpy:main Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

00 - Bug 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: busday_count returns wrong results with begindates > enddates

6 participants