1

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]])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    Does this answer your question? [Forced conversion of non-numeric numpy arrays with NAN replacement](https://stackoverflow.com/questions/16223483/forced-conversion-of-non-numeric-numpy-arrays-with-nan-replacement) – Tomerikoo Aug 24 '21 at 11:58
  • tks for help but doesn't work – jean_claude Aug 24 '21 at 12:08

2 Answers2

1

My solution iterates over the flattened array and checks if each type can be float. If not I will fill in a np.NaN instead. After this I will recreate the numpy array with dtype float into the old shape:

import numpy as np

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

shape = x.shape
x_nan = np.empty_like(x).flatten()
for i,x in enumerate(x.flat):
    try:
        x_nan[i] = float(x)
    except:
        x_nan[i] = np.NaN

x_nan = np.array(x_nan, dtype=np.float).reshape(shape)
print(x_nan)
# array([[ 1.,  2., nan],
#        [ 4., nan,  6.]])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

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.]]
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61