object
dtype arrays do not support vectorised operations. But you can do a round trip converting first to list
and then back to an array. Here we use the fact np.nan != np.nan
by design:
data = np.array([['beef', 'bread', 'cane_molasses', np.nan, np.nan, np.nan],
['brassica', 'butter', 'cardamom']])
res = np.array([[i for i in row if i == i] for row in data.tolist()])
array([['beef', 'bread', 'cane_molasses'],
['brassica', 'butter', 'cardamom']],
dtype='<U13')
Note the resultant array will be of string types (here with max length of 13). If you want an object
dtype array, which can hold arbitrary objects, you need to specify dtype=object
:
res = np.array([[i for i in row if i == i] for row in data.tolist()], dtype=object)
array([['beef', 'bread', 'cane_molasses'],
['brassica', 'butter', 'cardamom']], dtype=object)