How to convert an array of strings to an array of floats in numpy?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a NumPy array of strings into floating-point values is a common cleanup step after reading CSV files, command-line input, or loosely typed JSON data. The conversion itself is simple, but real datasets often contain whitespace, empty strings, or invalid numeric text that need deliberate handling.
The practical goal is to turn text into a numeric dtype without losing control over bad rows. In NumPy, the usual tools are np.array, np.asarray, and astype.
The Straightforward Conversion
If every element already contains a valid numeric string, the direct solution is astype(float):
This works because NumPy applies the conversion element by element and produces a new array with a floating-point dtype. In most cases, float becomes float64.
If the source is a regular Python list instead of a NumPy array, you can convert during array creation:
That avoids creating an intermediate string array.
np.asarray Versus np.array
If your input may already be a NumPy array, np.asarray is slightly more conservative because it reuses the existing array when possible:
Use this when you want array semantics but do not need to force an unconditional copy at creation time.
Handling Whitespace And Missing Values
Real data is rarely clean. Strings may contain leading spaces, trailing newlines, or blank entries. Stripping the text first makes conversion more reliable:
Blank strings are a different problem because astype(float) will raise ValueError. If you want missing values to become np.nan, normalize them explicitly:
That pattern is useful when missing numeric data is expected and downstream code can tolerate NaN values.
When Input May Be Invalid
If some entries may contain invalid text such as abc, fail-fast behavior from astype is often desirable because it surfaces bad input immediately. Still, there are cases where you want a controlled fallback. One clear approach is a small parser function:
This uses a Python loop, so it is not as fast as a pure vectorized conversion, but it is readable and handles messy input well.
Choosing The Right Float Type
By default, float gives you double precision. If memory matters and single precision is enough, request np.float32 explicitly:
This matters when you convert millions of values or prepare arrays for machine learning frameworks that commonly use float32.
Common Pitfalls
A common mistake is assuming astype(float) modifies the original array in place. It returns a new array. If you forget to assign the result, the source remains a string array.
Another pitfall is ignoring whitespace and sentinel values such as N/A, null, or empty strings. Clean those cases before conversion or the entire operation will fail.
A third issue is using Python float conversion in a manual loop for clean data that NumPy could convert directly. That works, but it gives up much of NumPy's convenience and performance without adding any benefit.
Summary
- Use
astype(float)when every string is already a valid number. - Use
dtype=floatduring array creation when you do not need an intermediate string array. - Clean whitespace and placeholder values before conversion.
- Map missing or invalid entries to
np.nanif downstream code can handle them. - Pick
np.float32ornp.float64deliberately based on precision and memory needs.

