0

I'm just failing to wrap my head around this. Basically it should be an easy thing to solve, but I'm not getting it right.

I set up an numpy array, and fill it with NaN values. Then I wanted to assign strings to it's fields (that is what I definitely want to do) I get "ValueError: could not convert string to float". All I found here in StackOverflow's search was how to convert the strings into floats - but that's not what I want to do.

Here's a small code example that describes what I'm talking about:

a = np.empty((6, 7))
a[:] = np.nan
# later in a loop
a[0,0] = 'abc'

Thanks in advance

Exi
  • 320
  • 1
  • 11

1 Answers1

1

Try specifying a dtype for the numpy array. a = np.empty((6, 7), dtype=object). The above error occurs as the dtype of the array a is float64. When you are trying to assign a string to a float array ValueError: could not convert string to float has been raised.

For further reference : How to create a numpy array of arbitrary length strings?

If this solution is helpful, kindly do upvote the solution. Thank you.

Kishore Sampath
  • 973
  • 6
  • 13