Lets say I have arrays a
and b
a = np.array([1,2,3])
b = np.array([\'red\',\'red\',\'red\'])
If I were to apply s
You can handle variable length strings by setting the dtype
of b
to be "object"
:
import numpy as np
a = np.array([1,2,3])
b = np.array(['red','red','red'], dtype="object")
b[a<3] = "blue"
print(b)
this outputs:
['blue' 'blue' 'red']
This dtype
will handle strings, or other general Python objects. This also necessarily means that under the hood you'll have a numpy
array of pointers, so don't expect the performance you get when using a primitive datatype.