3

Here is the code in question:

arr = ['-0.944', '0.472', '0.472']
charges = [np.float64(i) for i in arr] # [-0.944, 0.472, 0.472]
charges = np.ndarray(charges)

The error is thrown on the last step, where the list is casted to an ndarray. Assigning dtype=np.float64 in ndarray does not change the error. What is wrong with this snippet of code?

Numpy 1.14, Python 3.6.1

1
  • The constructor you're looking for is array, not ndarray. Calling ndarray directly is unusual. Commented Jun 25, 2018 at 16:07

1 Answer 1

4

The first argument to np.ndarray is the shape, which is usually a tuple of integers.

You should not use the low-level constructor np.ndarray. Correct interface is np.array, and take it directly from the strings without a list comprehension first:

>>> arr = ['-0.944', '0.472', '0.472']
>>> np.array(arr, dtype=np.float64)
array([-0.944,  0.472,  0.472])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks -- rookie mistake!

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.