8000 BUG: np.digitize casts integers to float64 · Issue #11022 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: np.digitize casts integers to float64 #11022

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

Open
eric-wieser opened this issue May 1, 2018 · 1 comment · Fixed by #11464 · May be fixed by #24449
Open

BUG: np.digitize casts integers to float64 #11022

eric-wieser opened this issue May 1, 2018 · 1 comment · Fixed by #11464 · May be fixed by #24449
Labels

Comments

@eric-wieser
Copy link
Member
eric-wieser commented May 1, 2018

Which leads to:

# all numbers appear between their successor and predecessor, right?
>>> check = lambda x: np.digitize(x, [x - 1, x + 1]) == 1

>>> check(1)
True
>>> check(2**52)
True
>> check(2**53)  # uh oh
False

The workaround is:

def digitize(x, bins, right=False):
    # arguments below are swapped, so this is swapped too
    if right:
        side = 'left' 
    else:
        side = 'right'
    return np.searchsorted(bins, x, side=side)

The issue right now is that the monotonicity detection in digitize is forcing everything to be case to float64. In almost all cases the user probably already sorted their input, so this is not only pointless overhead, but it's causing harmful behavior too.

eric-wieser added a commit to eric-wieser/numpy that referenced this issue May 1, 2018
eric-wieser added a commit to eric-wieser/numpy that referenced this issue Jul 6, 2018
This converts digitize to a pure-python function that falls back on searchsorted.

Performance doesn't really matter here anyway - if you care about performance, then you should just call searchsorted directly, rather than checking the order of the bins.

Partially fixes numpygh-11022
@eric-wieser
Copy link
Member Author

Still broken for decreasing bins - see the xfail in #11464

@eric-wieser eric-wieser reopened this Jul 8, 2018
LemonBoy added a commit to LemonBoy/numpy that referenced this issue Aug 18, 2023
Rewrite the underlying C implementation of the monotonicity check in a
generic fashion, allowing the use on arrays with comparable types.

Closes numpy#11022
LemonBoy added a commit to LemonBoy/numpy that referenced this issue Aug 18, 2023
Rewrite the underlying C implementation of the monotonicity check in a
generic fashion, allowing the use on arrays with comparable types.

Closes numpy#11022
@LemonBoy LemonBoy linked a pull request Aug 18, 2023 that will close this issue
LemonBoy added a commit to LemonBoy/numpy that referenced this issue Sep 7, 2023
Rewrite the underlying C implementation of the monotonicity check in a
generic fashion, allowing the use on arrays with comparable types.

Closes numpy#11022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant
0