Skip to content
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

Calling searchsorted on an int haystack with str needle converts haystack to str #6061

Open
levitsky opened this issue Jul 10, 2015 · 3 comments

Comments

@levitsky
Copy link
Contributor

With numpy 1.9.2 I see quite counter-intuitive behavior of searchsorted on integer arrays with a str needle:

In [1]: import numpy as np

In [2]: a = np.arange(1000, dtype=int)

In [3]: a.searchsorted('15')
Out[3]: 150

In [4]: a.searchsorted('150')
Out[4]: 150

In [5]: a.searchsorted('1500')
Out[5]: 151

In [6]: a.searchsorted('foo')
Out[6]: 1000

The main reason why I believe this behavior is broken is because when one converts an integer array to strings, it's not sorted anymore.
I believe that the least surprising result would be an exception, especially because this is what happens if the array contains floats and not integers.

P.S. I initially asked about this on StackOverflow, and @jaimefrio suggested creating a bug report in the comments.

@jaimefrio
Copy link
Member

I think fixing this boils down to whether it is a searchsorted bug, and we need to put a special check there, or it really is a can_cast bug, and we should disallow safe casting of ints to strings, as we already do for floats.

@njsmith
Copy link
Member

njsmith commented Jul 10, 2015

+1 to making int->str considered not-safe. Generally "safe" for us means
"should be implicitly coerceable", and python in general does not allow
implicit coercion of int->str, so this seems pretty correct to me.
On Jul 10, 2015 12:33 PM, "Jaime" notifications@github.com wrote:

I think fixing this boils down to whether it is a searchsorted bug, and
we need to put a special check there, or it really is a can_cast bug, and
we should disallow safe casting of ints to strings, as we already do for
floats.


Reply to this email directly or view it on GitHub
#6061 (comment).

vestuto added a commit to vestuto/numpy that referenced this issue Jul 11, 2015
- Fixes issue numpy#6061
- Updated _npy_can_cast_safely_table to define such casts as not safe.
- Updated existing python tests to be in agreement with this change.
- Added new test case to test_regression.py to check safe cast example cited in issue numpy#6061
@OmerJog
Copy link

OmerJog commented May 23, 2019

Is there a fix for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants