So i'm trying to add 2 numpy masked arrays together. The difficulty is that they have to be added as strings because im trying to get a binary code in the resulting output array. The code below is a simplified version of what i'm trying to do. The mask for both arrays will be the same (In practice these would be way larger arrays, but the idea is the same):
a = np.zeros((3,3))
b = np.ones((3,3))
amask = [[False,True,True],[True, True, False],[False, False , True]]
bmask = [[False,True,True],[True, True, False],[False, False , True]]
a = a.astype('str')
b= b.astype('str')
am = ma.masked_array(a,mask = amask)
bm = ma.masked_array(b, mask = bmask)
x = np.add(am,bm)
I would like the output to be something like :
[['01' -- --],[-- -- '01'],['01', '01' --]]
So it's very important for it to be strings, so they can be added as such.
Running this code however gives me the following error:
numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> None
Which I don't understand since both arrays clearly have the same datatypes in my opinion. Adding them without the string conversion works just fine but doesn't give me the required output. I have run into this error before and tried to look it up but never really understood it. Thanks
This isn't a masked array issue; it's a string dtype one.
astype(str)
makes an array with a numpy string dtype; this is optimized for array storage, but is not the same as Python strings.np.char
has some functions that can apply string methods toUn
dtypes:Or as commented, you can make a list like array of string objects:
For object dtype arrays, operators like
+
delegate the action to the methods of the elements. Equivalently:I expect [264] to be fastest.
numpy
doesn't add much to string processing.