Why does numpy silently convert my int array to strings when calling searchsorted?

I found a nasty bug in my code where I forgot to convert an integer from strto intbefore looking for it in a sorted array of integers. Having corrected this, I am still surprised that this did not raise an explicit exception.

Here is a demo:

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

With an array, floatthis does not work picking up TypeError: Cannot cast array data from dtype('float64') to dtype('<U32') according to the rule 'safe'.

My main question is: why does this not throw an exception for an integer array?

This is especially surprising since you can do both np.arange(1000, dtype=int).astype(str)and np.arange(1000, dtype=np.float64).astype(str, casting='safe').

Side questions:

  • why does it convert the whole array and not the argument?
  • Why does the search string convert to '<U32'?
+3
1

, searchsorted , . np.promote_types, (, ) :

>>> np.promote_types(int, str)
dtype('S11')

, dtypes , .

dtype, , np.can_cast. , float , ints:

In [1]: np.can_cast(np.float, np.promote_types(np.float, str))
Out[1]: False

In [2]: np.can_cast(np.int, np.promote_types(np.int, str))
Out[2]: True

, - , numeric + string = > string , int = > .

+5

Source: https://habr.com/ru/post/1621845/


All Articles