-1

I'm using the numpy broadcast function to map a set of values over a set of coordinates. The values can be of heterogeneous type, including primitives. My problem is that the broadcast function is converting the primitive types under certain conditions. See for example this code:

In [11]: x = np.array([1])
    ...: y = np.array(['test', 10])
    ...: list(np.broadcast(x, y))

Out[11]: [(1, 'test'), (1, '10')]

The int 10 that was passed in the y value has been converted to a string '10' after the broadcast.

Is it possible to prevent this casting behaviour somehow?

MarkNS
  • 3,811
  • 2
  • 43
  • 60

1 Answers1

2

This is because when you do

y = np.array(['test', 10])

the result is

array(['test', '10'], 
  dtype='<U4')

If you do y = np.array(['test', 10], dtype=object) the result will be as you wish.

P. Camilleri
  • 12,664
  • 7
  • 41
  • 76