Vikas Kyatannawar
Vikas Kyatannawar

Reputation: 136

How does type coercion for numpy arrays work?

Numpy imported as np here:

np.array([True, 1, 2]) + np.array([3, 4, False])

Which results in:

array([4, 5, 2])

In this example boolean changed to int. I assumed it's because of the operation.

np.array([True,1,2])

Which results in:

array(1,1,2)

Using a string seems it took the priority and changed int to string.

np.array(["True", 1, 2])

results in

array(['True', '1', '2'], dtype='<U4')

in [4]: np.array([0, 1, 2, 3, 'a', True])
Out[4]: 
array(['0', '1', '2', '3', 'a', 'True'], dtype='<U21')`

It now seems string takes highest priority, and not the number of objects of certain type. Why does it return dtype='<U4'/'U1'?

How exactly does this work?

Can I use this property to make change non-zero integers to be True and and zero as False or I have to use compare operator?

Upvotes: 1

Views: 1950

Answers (1)

yevgeniy
yevgeniy

Reputation: 908

The answer is partially contained in your question. Boolean type has implicit conversion to other numeric types since it is just 0 or 1, allowing e.g. arithmetic operations with masks. Numbers are also easily convertible to characters, giving you strings. But the other way around, assigning a str value to int does not work straight out of the box. That's why when you add a string to your array, it gets immediately generalized to the str type. When using dtype it returns U#, where # is dictated by the length of your string array, as far as I know, see the official docs here. Conversion from numeric arrays to bool works exactly as you specified, if an element is non-zero it returns True, otherwise False. Example:

a=np.array([0.0,1.5,0,1e8,-3])

for b in a:
    if b: print ("hi there")
    else: print ("go away!")

Effectively a values are treated as [False,True,False,True,True]

Upvotes: 1

Related Questions