How to convert a 2D array of strings and numbers to a numpy float array?

My array is 2D numpystr352 and contains a mix of numbers and strings. I want to convert it to float numpy so that the string elements should convert to nan (without using Pandas).

For example:

import numpy as np

x = np.array([[1, 2, 'tom'], [4, 'Manu', 6]])

What I want is:

x = np.array([[1, 2, nan], [4, nan, 6]])

Answer

Using genfromtext as suggested here is not possible with multi-dimensional arrays. On the other hand, you can apply genfromtext to each row of the 2D array using apply_along_axis as described here:

import numpy as np

x = np.array([[1, 2, 'tom'], [4, 'Manu', 6]])
print(np.apply_along_axis(np.genfromtxt, 1 ,x))

Which will give:

[[ 1.  2. nan]
 [ 4. nan  6.]]