Is there a native numpy way to convert an array of string representations of booleans eg:
['True','False','True','False']
To an actual boolean array I can use for masking/indexing? I could do a for loop going through and rebuilding the array but for large arrays this is slow.
Best answer
You should be able to do a boolean comparison, IIUC, whether the dtype
is a string or object
:
>>> a = np.array(['True', 'False', 'True', 'False'])
>>> a
array(['True', 'False', 'True', 'False'],
dtype='|S5')
>>> a == "True"
array([ True, False, True, False], dtype=bool)
or
>>> a = np.array(['True', 'False', 'True', 'False'], dtype=object)
>>> a
array(['True', 'False', 'True', 'False'], dtype=object)
>>> a == "True"
array([ True, False, True, False], dtype=bool)