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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that was what I was looking for dtype=object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.